【问题标题】:Hibernate Query to match mapped entity collection with at least one element in given collection, ManyToMany relationship休眠查询以匹配映射实体集合与给定集合中的至少一个元素,多对多关系
【发布时间】:2021-08-25 19:28:58
【问题描述】:

我的所有者实体:

@Entity(name = "SubscriptionEntity")
@Table(name = "SUBSCRIPTION", uniqueConstraints = {
        @UniqueConstraint(columnNames = "ID")})
 
public class SubscriptionEntity implements Serializable 
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private Integer subscriptionId;
 
    @Column(name = "SUBS_NAME", unique = true, nullable = false, length = 100)
    private String subscriptionName;
     
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="READER_SUBSCRIPTIONS", joinColumns={@JoinColumn(referencedColumnName="ID")}
                                        , inverseJoinColumns={@JoinColumn(referencedColumnName="ID")})
    private Set<ReaderEntity> readers;
 
    //Getters and setters
}

映射实体:

@Entity(name = "ReaderEntity")
@Table(name = "READER", uniqueConstraints = {
        @UniqueConstraint(columnNames = "ID"),
        @UniqueConstraint(columnNames = "EMAIL"),
        @UniqueConstraint(columnNames = "USERNAME"})
 
public class ReaderEntity implements Serializable 
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private Integer readerId;
 
    @Column(name = "EMAIL", unique = true, nullable = false, length = 100)
    private String email;
 
    @Column(name = "USERNAME", unique = false, nullable = false, length = 100)
    private String username;
 
    @ManyToMany(mappedBy="readers") 
    private Set<SubscriptionEntity> subscriptions;
 
    //Getters and setters
}

现在,我有一个 subscriptionList,其中包含很少的订阅。我想要一个ReaderEntity 对象的分页列表,其订阅至少属于subscriptionList 中的一个。即ReaderEntity.subscriptionssubscriptionList 的交集应该至少是一个。

我参考了这篇文章并写了一个查询: Hibernate or SQL Query M-N member of with collections?

@Query("SELECT r FROM ReaderEntity r LEFT JOIN r.subscriptions s WHERE (s.subscriptionName in (:subscriptionList))")
Page<User> findAllBySubscriptions(@Param("subscriptionList") Set<String> subscriptionList, Pageable pageable);

但如果subscriptionList 中的多个元素与实际ReaderEntity.subscriptions 匹配,则此查询会填充重复条目。

我不能使用Distinct,因为可分页包含排序顺序,它按用户名对列表进行排序,不区分大小写。所以它在末尾附加order by lower(username) 并抛出错误:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

谁能帮我制定这个查询或指导我如何实现这一目标?

【问题讨论】:

    标签: java sql database hibernate jpa


    【解决方案1】:

    使用提示pass distinct through

    @QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.PASS_DISTINCT_THROUGH, value = "false"))
    @Query("SELECT distinct r FROM ReaderEntity r LEFT JOIN r.subscriptions s WHERE (s.subscriptionName in (:subscriptionList))")
    Page<User> findAllBySubscriptions(@Param("subscriptionList") Set<String> subscriptionList, Pageable pageable);
    

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2012-04-07
      • 1970-01-01
      • 2021-11-11
      • 2018-09-22
      • 2011-05-05
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多