[英]Using an @Embeddable entity in a JPA CriteriaQuery
Let's say I have the following example entities - one is an @Embeddable
, embedded inside another @Entity
: 假设我有以下示例实体 - 一个是@Embeddable
,嵌入在另一个@Entity
:
@Embeddable
public class ContactInfoEntity {
@Column
private String phone;
@Column
private String zipCode;
}
@Entity
@Table(name = "EMPLOYEE")
public class EmployeeEntity {
@Id
@Column(name = "EMPLOYEE_ID")
private Long employeeId;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "phone",
column = @Column(name = "EMPLOYEE_PHONE")),
@AttributeOverride(name = "zipCode",
column = @Column(name = "EMPLOYEE_ZIP_CODE"))
})
private ContactInfoEntity employeeContactInfo;
}
The meta-model classes generated by the openjpa-maven-plugin include only an employeeContactInfo
variable, not the @AttributeOverride
columns. openjpa-maven-plugin生成的元模型类仅包含employeeContactInfo
变量,而不包括@AttributeOverride
列。
Now suppose I want to do this: 现在假设我想这样做:
Select the
EMPLOYEE_ID
andEMPLOYEE_PHONE
where theEMPLOYEE_ZIP_CODE
is equal to "123456" 选择EMPLOYEE_ID
和EMPLOYEE_PHONE
,其中EMPLOYEE_ZIP_CODE
等于“123456”
How do I create this as a CriteriaQuery
? 如何将其创建为CriteriaQuery
?
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<String> qDef = cb.createQuery(String.class);
Root<EmployeeEntity> e = qDef.from(EmployeeEntity.class);
qDef.select(e.get(EmployeeEntity_.employeeId),
e.get(????))
.where(cb.equal(e.get(????), "123456"));
return entityManager.createQuery(qDef).getResultList();
An example approach may look like this: 示例方法可能如下所示:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> qDef = cb.createQuery(Object[].class);
Root<EmployeeEntity> e = qDef.from(EmployeeEntity.class);
qDef.multiselect(
e.get(EmployeeEntity_.employeeId),
e.get(EmployeeEntity_.employeeContactInfo).get(ContactInfoEntity_.phone));
qDef.where(
cb.equal(
e.get(EmployeeEntity_.employeeContactInfo).get(ContactInfoEntity_.zipCode),
cb.literal("123456")));
List<Object[]> objects = em.createQuery(qDef).getResultList();
for (Object[] element : objects) {
System.out.format("%d %s", element[0], element[1]);
}
Depending on your preferences you may also want to get the results of the query as: 根据您的偏好,您可能还希望将查询结果视为:
constructor expression 构造函数表达式
public class EmployeeEntityResult { private int id; private String phone; public EmployeeEntityResult(int id, String phone) { this.id = id; this.phone = phone; } ... }
CriteriaQuery<EmployeeEntityResult> cq = cb.createQuery(EmployeeEntityResult.class); ... List<EmployeeEntityResult> result = em.createQuery(cq).getResultList(); for (EmployeeEntityResult element : result) { System.out.format("%d %s", element.getId(), element.getPhone()); }
tuple 元组
CriteriaQuery<Tuple> cq = cb.createTupleQuery(); ... cq.select( cb.tuple( e.get(EmployeeEntity_.employeeId) .alias("id"), e.get(EmployeeEntity_.employeeContactInfo).get(ContactInfoEntity_.phone) .alias("phone"))); ... List<Tuple> tuple = em.createQuery(cq).getResultList(); for (Tuple element : tuple) { System.out.format("%d %s", element.get("id"), element.get("phone")); }
The JPQL query looks as follows: JPQL查询如下所示:
SELECT e.id, e.employeeContactInfo.phone
FROM EmployeeEntity e
WHERE e.employeeContactInfo.zipCode = '123456'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.