Java 如何使用jpa标准api编写此查询? 选择c.id, 从customer_表c2中选择c2.value,其中c2.id=c.id,c2.key=test作为测试, 从customer_表c2中选择c2.value,其中c2.id=c.id,c2.key='service category'作为服务类别, 从customer_表c2中选择c2.value,其中c2.id=c.id,c2.key='exam'作为检查 来自客户_表c c.id分组;
假设customerTable实体、其关系和该值的类型为String,并正确建模,则实现如下:Java 如何使用jpa标准api编写此查询? 选择c.id, 从customer_表c2中选择c2.value,其中c2.id=c.id,c2.key=test作为测试, 从customer_表c2中选择c2.value,其中c2.id=c.id,c2.key='service category'作为服务类别, 从customer_表c2中选择c2.value,其中c2.id=c.id,c2.key='exam'作为检查 来自客户_表c c.id分组;,java,spring-boot,hibernate,jpa,criteria-api,Java,Spring Boot,Hibernate,Jpa,Criteria Api,假设customerTable实体、其关系和该值的类型为String,并正确建模,则实现如下: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<YourPojo> cq = cb.createQuery(YourPojo.class); Root<CustomerTable> root = cq.from(CustomerTable.class); //Subquery 1
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<YourPojo> cq = cb.createQuery(YourPojo.class);
Root<CustomerTable> root = cq.from(CustomerTable.class);
//Subquery 1
Subquery<String> sqVal1 = cq.subquery(String.class);
Root<CustomerTable> sq1Root = sqVal1.from(CustomerTable.class);
sqVal1.where(
cb.and(
cb.equal(root.get("id"),sq1Root.get("id")),
cb.equal(sq1Root.get("key"),"test")
)
);
sqVal1.select(sq1Root.get("value"));
//Subquery 2
Subquery<String> sqVal2 = cq.subquery(String.class);
Root<CustomerTable> sq2Root = sqVal2.from(CustomerTable.class);
sqVal2.where(
cb.and(
cb.equal(root.get("id"),sq2Root.get("id")),
cb.equal(sq2Root.get("key"),"service-category")
)
);
sqVal2.select(sq2Root.get("value"));
//Subquery 3
Subquery<String> sqVal3 = cq.subquery(String.class);
Root<CustomerTable> sq3Root = sqVal3.from(CustomerTable.class);
sqVal3.where(
cb.and(
cb.equal(root.get("id"),sq3Root.get("id")),
cb.equal(sq3Root.get("key"),"exam")
)
);
sqVal3.select(sq3Root.get("value"));
cq.groupBy(root.get("id"));
cq.multiselect(
root.get("id"),
sqVal1.getSelection(),
sqVal2.getSelection(),
sqVal3.getSelection()
);
建议使用元模型访问实体的属性,这将导致替换以下代码
root.get("id");
另一个
root.get(CustomerTable_.id);
使用元模型而不深入主题的许多优点之一是能够自动完成属性名称,并减少此时出错的机会。假设customerTable实体、其关系和该值的类型为String,并且建模正确,实施情况如下:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<YourPojo> cq = cb.createQuery(YourPojo.class);
Root<CustomerTable> root = cq.from(CustomerTable.class);
//Subquery 1
Subquery<String> sqVal1 = cq.subquery(String.class);
Root<CustomerTable> sq1Root = sqVal1.from(CustomerTable.class);
sqVal1.where(
cb.and(
cb.equal(root.get("id"),sq1Root.get("id")),
cb.equal(sq1Root.get("key"),"test")
)
);
sqVal1.select(sq1Root.get("value"));
//Subquery 2
Subquery<String> sqVal2 = cq.subquery(String.class);
Root<CustomerTable> sq2Root = sqVal2.from(CustomerTable.class);
sqVal2.where(
cb.and(
cb.equal(root.get("id"),sq2Root.get("id")),
cb.equal(sq2Root.get("key"),"service-category")
)
);
sqVal2.select(sq2Root.get("value"));
//Subquery 3
Subquery<String> sqVal3 = cq.subquery(String.class);
Root<CustomerTable> sq3Root = sqVal3.from(CustomerTable.class);
sqVal3.where(
cb.and(
cb.equal(root.get("id"),sq3Root.get("id")),
cb.equal(sq3Root.get("key"),"exam")
)
);
sqVal3.select(sq3Root.get("value"));
cq.groupBy(root.get("id"));
cq.multiselect(
root.get("id"),
sqVal1.getSelection(),
sqVal2.getSelection(),
sqVal3.getSelection()
);
建议使用元模型访问实体的属性,这将导致替换以下代码
root.get("id");
另一个
root.get(CustomerTable_.id);
使用元模型而不深入主题的许多优点之一是能够自动完成属性名称,并减少此时出错的机会