我们说我有以下示例实体 - 一个是@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;
}
openjpa-maven-plugin生成的元模型类仅包含employeeContactInfo
变量,而不包括@AttributeOverride
列。
现在假设我想这样做:
选择
EMPLOYEE_ID
和EMPLOYEE_PHONE
EMPLOYEE_ZIP_CODE
等于" 123456"
如何将其创建为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();
答案 0 :(得分:4)
示例方法可能如下所示:
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]);
}
根据您的偏好,您可能还希望将查询结果显示为:
构造函数表达式
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());
}
元组
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"));
}
JPQL查询如下所示:
SELECT e.id, e.employeeContactInfo.phone
FROM EmployeeEntity e
WHERE e.employeeContactInfo.zipCode = '123456'