问题描述
我试图根据实体列表中的某些条件提取"可嵌入类.借助JPQL或Criteria API.我不是专业人士,所以请帮帮我.谷歌搜索了4个小时,没有任何结果.
I am trying to "extract" Embeddable classes based on some criterias from a list in an Entity. Either with the help of JPQL or Criteria API. I am not a pro at this, so please help me out. Been googling for 4 hours solid for an answer without any results.
这些是类:
@Entity
public class PostOffice {
@Id
private Long id;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(joinColumns=@JoinColumn(name = "CARRIERID"))
private List<PostalCarrier> carriers;
}
@Embeddable
public class PostalCarrier {
@JoinColumn(name = "area")
private Area area;
}
@Entity
public class Area {
@Id
private int code;
}
所以,基本上我想要达到的目标是这样的.
So, basically what I'm trying to achieve is something like this.
TypedQuery<PostalCarrier> query = entityManager.createQuery("SELECT p.carriers FROM PostOffice p
WHERE p.id = ?1 AND p.carriers.area.code = ?2", PostalCarrier.class);
query.setParameter(1, postOfficeId);
query.setParameter(2, areaCode);
我只想从特定的PostOffice获得特定地区代码的PostalCarriers列表.任何帮助深表感谢! :)
I only want to get a list of the PostalCarriers at specific area code from a specific PostOffice.Any help is much appreciated! :)
我想我快到了,但是继续出现以下错误:
I think I'm almost there, but keep getting the following error:
Error compiling the query [SELECT h FROM PostOffice p INNER JOIN p.carriers h
WHERE p.id = ?1 AND h.area.code = ?2], line 1, column 71: unknown state or
association field [area] of class [com.test.PostalCarrier].
推荐答案
您必须加入PostalCarrier.您无法访问集合中的属性.因此,postalcarrier.area
是不正确的.
You must make a join to the PostalCarrier. You can't access a property from a collection. Thus, postalcarrier.area
isn't correct.
select postalcarrier from PostOffice p
inner join p.carriers postalcarrier
where p.id = :postOfficeId
and postalcarrier.area.code = :areaCode
这篇关于JPA:查询实体内的可嵌入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!