gpt4 book ai didi

JPA - Criteria API 和 EmbeddedId

转载 作者:行者123 更新时间:2023-12-03 02:15:27 24 4
gpt4 key购买 nike

我想使用条件进行以下查询。我有一个定义了 EmbeddedIdEntity:

 @Entity
@Table(name="TB_INTERFASES")
public class Interfase implements Serializable {

@EmbeddedId
private InterfaseId id;
}

@Embeddable
public class InterfaseId implements Serializable {
@Column(name="CLASE")
private String clase;
}

我想做的条件查询是:

 CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Interfase> criteriaQuery = criteriaBuilder.createQuery(Interfase.class);
Root<Interfase> entity = criteriaQuery.from(Interfase.class);
criteriaQuery.where(
criteriaBuilder.equal(entity.get("clase"), "Clase"),
);

但这会引发 IllegalArgumentException:

java.lang.IllegalArgumentException: Not an managed type: class InterfaseId

我也尝试过这个查询:

 Root<Interfase> entity = criteriaQuery.from(Interfase.class);
criteriaQuery.where(
criteriaBuilder.equal(entity.get("id").get("clase"), "Clase"),
);

还有这个...

 Root<Interfase> entity = criteriaQuery.from(Interfase.class);
criteriaQuery.where(
criteriaBuilder.equal(entity.get("id.clase", "Clase"),
);

没有运气。所以我的问题是,当我的类使用 Embedded 和 EmbeddedId 注释时,如何使用条件进行查询?

谢谢!毛罗。

最佳答案

您需要使用路径导航来访问Embeddable的属性。以下是 JPA 2.0 规范中的示例(使用静态元模型):

6.5.5 Path Navigation

...

In the following example, ContactInfo is an embeddable class consisting of an address and set of phones. Phone is an entity.

CriteriaQuery<Vendor> q = cb.createQuery(Vendor.class);
Root<Employee> emp = q.from(Employee.class);
Join<ContactInfo, Phone> phone =
emp.join(Employee_.contactInfo).join(ContactInfo_.phones);
q.where(cb.equal(emp.get(Employee_.contactInfo)
.get(ContactInfo_.address)
.get(Address_.zipcode), "95054"))
.select(phone.get(Phone_.vendor));

The following Java Persistence query language query is equivalent:

SELECT p.vendor
FROM Employee e JOIN e.contactInfo.phones p
WHERE e.contactInfo.address.zipcode = '95054'

所以就你的情况而言,我认为你需要这样的东西:

criteriaBuilder.equal(entity.get("id").get("clase"), "Referencia 111")

引用文献

  • JPA 2.0 规范
    • 第 6.5.5 节“路径导航”
<小时/>

更新:我已经使用 Hibernate EntityManager 3.5.6 和以下查询测试了提供的实体:

CriteriaBuilder builder = em.getCriteriaBuilder();

CriteriaQuery<Interfase> criteria = builder.createQuery(Interfase.class);
Root<Interfase> interfaseRoot = criteria.from(Interfase.class);
criteria.select(interfaseRoot);
criteria.where(builder.equal(interfaseRoot.get("id").get("clase"),
"Referencia 111"));

List<Interfase> interfases = em.createQuery(criteria).getResultList();

运行良好并生成以下 SQL:

17:20:26.893 [main] DEBUG org.hibernate.SQL -     select        interfase0_.CLASE as CLASE31_     from        TB_INTERFASES interfase0_     where        interfase0_.CLASE=?17:20:26.895 [main] TRACE org.hibernate.type.StringType - binding 'Referencia 111' to parameter: 1

按预期工作。

关于JPA - Criteria API 和 EmbeddedId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4084431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com