java - 在 Hibernate 中使用不同别名多次连接到同一关联

标签 java hibernate jpa orm hql

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

我有五个实体:ThreadThreadParticipantParticipantAccountCompany.每个消息线程至少有两个参与者,其中一个与用户(Account 实体)关联,另一个与Company 关联。该约束由应用程序强制执行。数据库这样设计是为了支持 future 的功能。数据库中给定线程的两个参与者的示例如下所示:

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 的两次提取,我收到异常无法同时提取多个包。如果我在这里只执行一次联接,生成的 SQL 会忽略我的附加联接,并且仅联接参与者一次,这要求参与者同时拥有关联的帐户和公司。使用原始 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 做我想做的事情,还是必须使用 native 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;

关于java - 在 Hibernate 中使用不同别名多次连接到同一关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29122326/

相关文章:

用于验证字符串的 Java 正则表达式

java - 为什么 JIT 会在启动时编译一些方法?

java - 让 hibernate 记录 clob 参数

hibernate - Grails/hibernate 标准ID inList

java - org.hibernate.ObjectDeletedException : deleted object would be re-saved by cascade : Error while trying to delete a Patient object

java - 在 Spring 中将 PersistenceException 转换为 DataAccessException

java - 监视 Java Hibernate 数据检索

Java JSONPARSER,验证JSON(解析之前或之后)

java - 将Java代码转换为NodeJS - 加密方法

java - 使用 Hibernate 将实体映射到物化 View