获取具有一对多关系的所有实体,其中一个相关实体满足条件
Get all entities with a one to many relationship where one of the related entities meets criteria
我需要根据一对多关系中相关记录的约束来获取记录。例如,我有:
@Entity
@Table (name = "LISTING")
public class Listing
{
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "LISTING_SEQ")
@SequenceGenerator (name = "LISTING_SEQ", initialValue = 1, allocationSize = 1, sequenceName = "LISTING_SEQ")
@Column (unique = true, nullable = false, updatable = false)
long id;
@OneToMany (mappedBy = "listing", fetch = FetchType.EAGER)
Set<ListingLineItem> listingLineItems;
...
}
还有……
@Entity
@Table (name = "LISTING_LINE_ITEM")
public class ListingLineItem
{
@EmbeddedId
ListingLineItemPK id;
boolean ignored;
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn (name = "listing_id", nullable = false)
Listing listing;
...
}
我需要编写一个 JPQL/HQL 查询或利用一个 CriteriaBuilder
给我 Listing
,其中有一个相关的 ListingLineItem,其中忽略 = true。
这里是 jpql 中的解决方案:
SELECT l FROM Listing l, ListingLineItem lli
WHERE lli.listing.id = l.id AND lli.ignored = true
我需要根据一对多关系中相关记录的约束来获取记录。例如,我有:
@Entity
@Table (name = "LISTING")
public class Listing
{
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "LISTING_SEQ")
@SequenceGenerator (name = "LISTING_SEQ", initialValue = 1, allocationSize = 1, sequenceName = "LISTING_SEQ")
@Column (unique = true, nullable = false, updatable = false)
long id;
@OneToMany (mappedBy = "listing", fetch = FetchType.EAGER)
Set<ListingLineItem> listingLineItems;
...
}
还有……
@Entity
@Table (name = "LISTING_LINE_ITEM")
public class ListingLineItem
{
@EmbeddedId
ListingLineItemPK id;
boolean ignored;
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn (name = "listing_id", nullable = false)
Listing listing;
...
}
我需要编写一个 JPQL/HQL 查询或利用一个 CriteriaBuilder
给我 Listing
,其中有一个相关的 ListingLineItem,其中忽略 = true。
这里是 jpql 中的解决方案:
SELECT l FROM Listing l, ListingLineItem lli
WHERE lli.listing.id = l.id AND lli.ignored = true