[英]Using dynamic calls as criteria for a jpa call
In the following code we get the data from specific entity (Pet). 在以下代码中,我们从特定实体(Pet)获取数据。 Can I use this code to get dynamically any entity (instead of pet use customer ,order etc)
我可以使用此代码动态获取任何实体(而不是宠物使用客户,订单等)
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
Root<Pet> pet = cq.from(Pet.class);
cq.where(cb.equal(pet.get("name"), "Fido"));
TypedQuery<Pet> q = em.createQuery(cq);
List<Pet> results = q.getResultList();
Yes, of course: 当然是:
public <T> List<T> getEntitiesNamedFido(Class<T> clazz) {
CriteriaQuery<T> cq = cb.createQuery(clazz);
Root<T> root = cq.from(clazz);
cq.where(cb.equal(root.get("name"), "Fido"));
TypedQuery<T> q = em.createQuery(cq);
List<T> results = q.getResultList();
}
Now pass any entity class having a persistent "name" property, and you'll get a list of the entity instances named Fido. 现在传递任何具有持久“name”属性的实体类,您将获得名为Fido的实体实例列表。
在函数上使用模板并通过参数传递类类型,参数名称和值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.