在 Hibernate 中多个连接到具有不同别名的相同关联
Multiple joins to same association with different aliases in Hibernate
我正在使用 Spring MVC 和 Spring Data JPA 以及 Hibernate 作为我的 JPA 提供程序,为我的 Web 应用程序构建一个消息传递系统。
我有五个实体:Thread
、ThreadParticipant
、Participant
、Account
和 Company
。每个消息线程至少有两个参与者,其中一个与用户(Account
实体)关联,另一个与 Company
关联。此约束由应用程序强制执行。数据库是这样设计的,以支持未来的功能。数据库中给定线程的两个参与者的示例如下所示:
id account_id company_id
1 44 NULL
2 NULL 123
id=1的行是用户,id=2的行是公司。我想要做的是编写一个 HQL 查询,提取给定帐户的所有 Thread
对象,同时包含 user/account 参与者和公司参与者。我尝试为我的联接使用不同的别名,如下所示:
select distinct t
from Thread t
inner join fetch t.threadParticipants user_tp
inner join fetch t.threadParticipants company_tp
inner join fetch user_tp.participant user_p
inner join fetch user_p.account a
inner join fetch company_tp.participant receiver_p
inner join fetch receiver_p.company
where a.id = :accountId
由于 t.threadParticipants
的两次提取,我得到异常 cannot simultaneously fetch multiple bags
。如果我在这里只进行一次加入,生成的 SQL 会忽略我的额外加入,只加入一次 Participant
,这需要参与者同时拥有一个帐户和一个关联的公司。使用原始 SQL,我可以这样做,而且效果很好:
select *
from thread t
inner join thread_participant user_tp on (user_tp.thread_id = t.id)
inner join thread_participant company_tp on (company_tp.thread_id = t.id)
inner join participant user_p on (user_p.id = user_tp.participant_id)
inner join account a on (a.id = user_p.account_id)
inner join participant company_p on (company_p.id = company_tp.participant_id)
inner join company c on (c.id = company_p.company_id)
where a.id = 123;
如果我不对同一个 table 使用不同的别名(请参阅下面的查询),查询运行正常,但我只得到返回的线程参与者之一 - 与帐户。
select distinct t
from Thread t
inner join fetch t.threadParticipants tp
inner join fetch tp.participant p
inner join fetch p.account a
left join fetch p.company
where a.id = :accountId
有什么方法可以让我用 HQL 做我想做的事,还是我必须使用本机 SQL?
我的映射如下:
线程实体
@Entity
@Table(name = "thread")
public class Thread {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "thread", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Collection<ThreadParticipant> threadParticipants = new HashSet<>();
// Getters and setters
}
参与实体
@Entity
@Table(name = "participant")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Account.class, cascade = { CascadeType.PERSIST })
@JoinColumn(name = "account_id")
private Account account;
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Company.class)
@JoinColumn(name = "company_id")
private Company company;
// Getters and setters
}
ThreadParticipant 实体
@Entity
@Table(name = "thread_participant")
@IdClass(ThreadParticipantPK.class)
public class ThreadParticipant implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Participant.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "participant_id")
private Participant participant;
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Thread.class)
@JoinColumn(name = "thread_id")
private Thread thread;
@Column(name = "last_viewed", nullable = true)
private Date lastViewed;
// Getters and setters
}
ThreadParticipantPK
public class ThreadParticipantPK implements Serializable {
private Thread thread;
private Participant participant;
public ThreadParticipantPK() { }
public ThreadParticipantPK(Thread thread, Participant participant) {
this.thread = thread;
this.participant = participant;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ThreadParticipantPK)) return false;
ThreadParticipantPK that = (ThreadParticipantPK) o;
if (!participant.equals(that.participant)) return false;
if (!thread.equals(that.thread)) return false;
return true;
}
@Override
public int hashCode() {
int result = thread.hashCode();
result = 31 * result + participant.hashCode();
return result;
}
// Getters and setters
}
提前致谢!
尝试将 threadParticipants
集合的类型更改为 Set
而不是 Collection
:
private Set<ThreadParticipant> threadParticipants;
我正在使用 Spring MVC 和 Spring Data JPA 以及 Hibernate 作为我的 JPA 提供程序,为我的 Web 应用程序构建一个消息传递系统。
我有五个实体:Thread
、ThreadParticipant
、Participant
、Account
和 Company
。每个消息线程至少有两个参与者,其中一个与用户(Account
实体)关联,另一个与 Company
关联。此约束由应用程序强制执行。数据库是这样设计的,以支持未来的功能。数据库中给定线程的两个参与者的示例如下所示:
id account_id company_id
1 44 NULL
2 NULL 123
id=1的行是用户,id=2的行是公司。我想要做的是编写一个 HQL 查询,提取给定帐户的所有 Thread
对象,同时包含 user/account 参与者和公司参与者。我尝试为我的联接使用不同的别名,如下所示:
select distinct t
from Thread t
inner join fetch t.threadParticipants user_tp
inner join fetch t.threadParticipants company_tp
inner join fetch user_tp.participant user_p
inner join fetch user_p.account a
inner join fetch company_tp.participant receiver_p
inner join fetch receiver_p.company
where a.id = :accountId
由于 t.threadParticipants
的两次提取,我得到异常 cannot simultaneously fetch multiple bags
。如果我在这里只进行一次加入,生成的 SQL 会忽略我的额外加入,只加入一次 Participant
,这需要参与者同时拥有一个帐户和一个关联的公司。使用原始 SQL,我可以这样做,而且效果很好:
select *
from thread t
inner join thread_participant user_tp on (user_tp.thread_id = t.id)
inner join thread_participant company_tp on (company_tp.thread_id = t.id)
inner join participant user_p on (user_p.id = user_tp.participant_id)
inner join account a on (a.id = user_p.account_id)
inner join participant company_p on (company_p.id = company_tp.participant_id)
inner join company c on (c.id = company_p.company_id)
where a.id = 123;
如果我不对同一个 table 使用不同的别名(请参阅下面的查询),查询运行正常,但我只得到返回的线程参与者之一 - 与帐户。
select distinct t
from Thread t
inner join fetch t.threadParticipants tp
inner join fetch tp.participant p
inner join fetch p.account a
left join fetch p.company
where a.id = :accountId
有什么方法可以让我用 HQL 做我想做的事,还是我必须使用本机 SQL?
我的映射如下:
线程实体
@Entity
@Table(name = "thread")
public class Thread {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "thread", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Collection<ThreadParticipant> threadParticipants = new HashSet<>();
// Getters and setters
}
参与实体
@Entity
@Table(name = "participant")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Account.class, cascade = { CascadeType.PERSIST })
@JoinColumn(name = "account_id")
private Account account;
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Company.class)
@JoinColumn(name = "company_id")
private Company company;
// Getters and setters
}
ThreadParticipant 实体
@Entity
@Table(name = "thread_participant")
@IdClass(ThreadParticipantPK.class)
public class ThreadParticipant implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Participant.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "participant_id")
private Participant participant;
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Thread.class)
@JoinColumn(name = "thread_id")
private Thread thread;
@Column(name = "last_viewed", nullable = true)
private Date lastViewed;
// Getters and setters
}
ThreadParticipantPK
public class ThreadParticipantPK implements Serializable {
private Thread thread;
private Participant participant;
public ThreadParticipantPK() { }
public ThreadParticipantPK(Thread thread, Participant participant) {
this.thread = thread;
this.participant = participant;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ThreadParticipantPK)) return false;
ThreadParticipantPK that = (ThreadParticipantPK) o;
if (!participant.equals(that.participant)) return false;
if (!thread.equals(that.thread)) return false;
return true;
}
@Override
public int hashCode() {
int result = thread.hashCode();
result = 31 * result + participant.hashCode();
return result;
}
// Getters and setters
}
提前致谢!
尝试将 threadParticipants
集合的类型更改为 Set
而不是 Collection
:
private Set<ThreadParticipant> threadParticipants;