当前位置: 首页 > 知识库问答 >
问题:

M: N未使用Hibernate查询获取关联

韦阳晖
2023-03-14

我通过一个实体(因为关联有一个附加字段),在线程和参与者之间有一个多对多的关系。我有以下映射。

线程实体

@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
}

现在,我尝试使用以下查询(使用Spring数据JPA)获取线程,并将Hibernate作为我的JPA提供者。

@Repository
public interface ThreadRepository extends JpaRepository<Thread, Integer> {
    @Query("select distinct t from Thread t inner join fetch t.threadParticipants tp inner join fetch tp.participant p left join fetch p.account a left join fetch p.company c")
    public List<Thread> test();
}

问题是,当ThreadParticipants中的关联的获取类型设置为FetchType时。懒惰的线程。threadParticipants集合为空。因此,如果我将关联设置为FetchType。渴望线程。threadParticipants包含两个元素(应该是这样的)。然而,在这种情况下,Hibernate会执行四个SQL查询来获取单个线程。

Hibernate: select thread0_.id as id1_18_0_, threadpart1_.participant_id as particip2_19_1_, threadpart1_.thread_id as thread_i3_19_1_, participan2_.id as id1_12_2_, account3_.id as id1_0_3_, company4_.id as id1_6_4_, thread0_.created as created2_18_0_, thread0_.last_activity as last_act3_18_0_, thread0_.subject as subject4_18_0_, threadpart1_.last_viewed as last_vie1_19_1_, threadpart1_.thread_id as thread_i3_18_0__, threadpart1_.participant_id as particip2_19_0__, threadpart1_.thread_id as thread_i3_19_0__, participan2_.account_id as account_2_12_2_, participan2_.company_id as company_3_12_2_, account3_.email as email2_0_3_, account3_.facebook_profile_id as facebook3_0_3_, account3_.first_name as first_na4_0_3_, account3_.last_name as last_nam5_0_3_, account3_.middle_name as middle_n6_0_3_, company4_.additional_address_text as addition2_6_4_, company4_.banner_name as banner_n3_6_4_, company4_.ci_number as ci_numbe4_6_4_, company4_.city_id as city_id22_6_4_, company4_.co_name as co_name5_6_4_, company4_.company_type_code as company_6_6_4_, company4_.created as created7_6_4_, company4_.description as descript8_6_4_, company4_.email as email9_6_4_, company4_.last_modified as last_mo10_6_4_, company4_.logo_name as logo_na11_6_4_, company4_.name as name12_6_4_, company4_.number_of_reviews as number_13_6_4_, company4_.phone_number as phone_n14_6_4_, company4_.postal_box as postal_15_6_4_, company4_.rating as rating16_6_4_, company4_.second_phone_number as second_17_6_4_, company4_.street_name as street_18_6_4_, company4_.street_number as street_19_6_4_, company4_.teaser as teaser20_6_4_, company4_.website as website21_6_4_ from thread thread0_ inner join thread_participant threadpart1_ on thread0_.id=threadpart1_.thread_id inner join participant participan2_ on threadpart1_.participant_id=participan2_.id left outer join account account3_ on participan2_.account_id=account3_.id left outer join company company4_ on participan2_.company_id=company4_.id
Hibernate: select participan0_.id as id1_12_0_, participan0_.account_id as account_2_12_0_, participan0_.company_id as company_3_12_0_ from participant participan0_ where participan0_.id=?
Hibernate: select thread0_.id as id1_18_0_, thread0_.created as created2_18_0_, thread0_.last_activity as last_act3_18_0_, thread0_.subject as subject4_18_0_ from thread thread0_ where thread0_.id=?
Hibernate: select participan0_.id as id1_12_0_, participan0_.account_id as account_2_12_0_, participan0_.company_id as company_3_12_0_ from participant participan0_ where participan0_.id=?

显然,它为每个参与者执行一个查询,为每个线程执行两个查询。因此,没有FetchType。迫不及待,我的代码根本不起作用,但有了它,我的数据库就会被杀死。我尝试在参与者和线程参与者之间添加一个关联(类似于从线程到线程参与者),但结果相同。我还尝试将所有别名添加到查询的字段列表中,但没有成功。

为什么会这样?我的映射或查询是否错误?提前谢谢!


共有1个答案

傅翰池
2023-03-14

尝试将一对多集合更改为集合:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "thread", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<ThreadParticipant> threadParticipants = new HashSet<>();

如果您只有懒关联,则不应生成额外的查询。这可能是因为默认情况下,多对一和一对一的关系是急切的。试着将它们设置为懒惰。

 类似资料:
  • 问题内容: 我有一个对象(BlogPost),其中包含元素(标记)的M:N集合。 如何查询对象(BlogPost)中至少一个对象的标签与使用JPA2(hibernate)的一组标签(由用户定义)中的元素匹配。 我的主要问题是,我实际上需要比较两个标签集合:-BlogPost的标签集合。-我搜索的收藏集 我尝试了一下,但是它不起作用,因为我的帖子实体具有多个标签。 那我该怎么办呢? 我的BlogPo

  • 我正在为实体对象上的延迟加载集合执行此操作: 我想返回一个实体对象,其中加载了多个延迟加载的集合,我可以这样做吗(传入一个列表并为单个条件设置多个关联?):

  • 问题内容: 我不确定如何描述这个问题,所以我认为举个例子是问我问题的最好方法: 我有两个表具有多对多关系: 驾驶执照<-> LicenceClass LicenceClass是“汽车”,“摩托车”和“中型刚性”之类的东西。 使用Hibernate Criteria,如何找到同时具有“汽车”和“摩托车” LicenceClasses的所有许可证? 2008年12月11日更新我发现可以使用自定义Res

  • 问题内容: 我需要从Hibernate查询中获取字符串,并稍后进行处理(因此无法使用来解决)。 我已经看过如何从HibernateCriteriaAPI(并非用于记录日志)中获取SQL,但是有了这种解决方法,我得到了SQL查询字符串,但是没有显示它显示’?’的参数值,有没有办法获得带有参数值的完整SQL字符串? 我的意思是,有了这种解决方案,我可以得到,但我需要得到… 有想法吗? 问题答案: 有一

  • 我在代码中使用条件查询。它总是激发 相反,我想忽略查询中的一列(字段),因为该字段以字节形式存储了大量数据。导致性能问题。 有谁能给出一个主意吗? 一些更新 我在查询中添加了一个投影,它创建了一个类似... 现在问题就像..中的未知列“y4_”以及y8_和y5_的相同错误意味着关闭它给出错误的所有内容。 我把它修改成像... 而且奏效了。但是不知道在HQL里面怎么修改?