JPA:查询实体内的可嵌入列表

发布于 2025-01-01 22:58:31 字数 1159 浏览 0 评论 0原文

我正在尝试根据实体列表中的某些条件“提取”可嵌入类。借助 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-08 22:58:31

您必须加入 PostalCarrier。您无法从集合中访问属性。因此,postalrier.area 不正确。

select postalcarrier from PostOffice p
inner join p.carriers postalcarrier
where p.id = :postOfficeId
and postalcarrier.area.code = :areaCode

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文