我的所有者实体:
@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
}
Map实体:
@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
. i、 e.交叉点 ReaderEntity.subscriptions
及 subscriptionList
应该至少有一个。
我参考了这篇文章并编写了一个查询:hibernate或sql查询m-n成员的集合?
@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);
这就实现了我的目标,即获取至少有一个共同点的读者 ReaderEntity.subscriptions
及 subscriptionList
.
我想要达到的目标:
现在,我有一个 restrictedSubscriptionList
这是一个 SubscriptionEntity
(可以是 SubscriptionEntity.name
). 现在我想扩展上面的查询,这样,如果读者订阅了 restrictedSubscriptionList
,则不会被提取。也就是说,如果 ReaderEntity.subscriptions
及 restrictedSubscriptionList
如果不为null,则不应提取读取器。
到目前为止我尝试的是:我尝试添加 AND NOT IN (:restrictedSubscriptionList)
但它似乎有错误的逻辑,并且仍然使用至少订阅了一个restrictedsubscriptionlist来填充所有读者。
我手动获取了属于这些人的读者 restrictedSubscriptionList
并将其添加为排除: WHERE r.username NOT IN (:restrictedSubscriptionList)
但是,当这样的读者越来越多时,查询将变得巨大,导致效率降低。
如果有人能帮我提出这个问题,我将不胜感激。
非常感谢您抽出时间阅读我的问题。
暂无答案!
目前还没有任何答案,快来回答吧!