多个连接到Hibernate中具有不同别名的相同关联

时间:2015-03-18 12:39:31

标签: java hibernate jpa orm hql

我正在为我的Web应用程序构建一个消息传递系统,使用Spring MVC,Spring Data JPA和Hibernate作为我的JPA提供者。

我有五个实体:ThreadThreadParticipantParticipantAccountCompany。每个消息线程至少有两个参与者,其中一个与用户(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

由于两次cannot simultaneously fetch multiple bags次提取,我收到异常t.threadParticipants。如果我在这里只进行一次连接,生成的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;

如果我没有为同一个表使用不同的别名(请参阅下面的查询),则查询运行正常,但我只返回一个返回的线程参与者 - 与该帐户关联的一个。 / p>

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
}

提前谢谢!

1 个答案:

答案 0 :(得分:2)

尝试将threadParticipants集合的类型更改为Set而不是Collection

private Set<ThreadParticipant> threadParticipants;