JPA:查询实体内的可嵌入列表
我正在尝试根据实体列表中的某些条件“提取”可嵌入类。借助 JPQL 或 Criteria API。我不是这方面的专家,所以请帮助我。已经用谷歌搜索了 4 个小时,没有任何结果。
这些是课程:
@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;
}
所以,基本上我想要实现的目标是这样的。
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);
我只想从特定邮局获取特定区号的邮政运营商列表。 非常感谢任何帮助! :)
我想我已经快到了,但是不断收到以下错误:
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].
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.
These are the classes:
@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);
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].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须加入 PostalCarrier。您无法从集合中访问属性。因此,
postalrier.area
不正确。You must make a join to the PostalCarrier. You can't access a property from a collection. Thus,
postalcarrier.area
isn't correct.