【发布时间】:2015-05-21 06:04:30
【问题描述】:
我正在使用带有 Spring Data JPA 和 Hibernate 作为我的 JPA 提供程序的 Spring MVC 为我的 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 对象,其中包含用户/帐户参与者以及公司参与者。我尝试为我的联接使用不同的别名,如下所示:
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;
如果我不对同一个表使用不同的别名(请参阅下面的查询),则查询运行良好,但我只返回了一个线程参与者 - 与帐户关联的那个。
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
}
线程参与者实体
@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
}
提前谢谢你!
【问题讨论】:
标签: java hibernate jpa orm hql