简体   繁体   English

JPA使用where子句选择行

[英]JPA select rows with where clause

I know that entityManager.find() method can be used to fetch a row with the primary key. 我知道entityManager.find()方法可用于获取具有主键的行。

But I have a requirement to define a common find method which accepts a map having the where clause conditions. 但是我需要定义一个通用的find方法,该方法接受具有where子句条件的映射。 In the map, key will be the column name and value will be the column name value of the where clause. 在映射中,键将是列名,值将是where子句的列名值。 This method should return the list of selected rows. 此方法应返回所选行的列表。

Can some one help me out? 有人可以帮我吗?

Take a look at: EntityManager.createQuery . 看一下: EntityManager.createQuery If I understand your question correctly, this will allow you to create the query that you would like to execute. 如果我正确理解您的问题,这将使您可以创建要执行的查询。 You could also, take a look at using a CriteriaBuilder . 您也可以看一下使用CriteriaBuilder的方法

Following might be overkill, but is a fairly generic way to approach it utilizing the criteriaBuilder. 跟随可能是过大的,但是是使用criteriaBuilder处理它的一种相当通用的方法。 While I can't paste the code here (work) I created an abstract BaseFilter<Entity> class. 虽然无法在此处粘贴代码(工作),但我创建了一个抽象BaseFilter <Entity>类。

The easy part is then having the implementing objects provide getXX, setXX properties. 然后,简单的部分是使实现对象提供getXX,setXX属性。 A getPredicates() was then added to return an ArrayList of predicates that the abstract BaseDAO could then invoke to perform the query. 然后添加了getPredicates()以返回谓词ArrayList,然后抽象BaseDAO可以调用该谓词来执行查询。

We worked specifically with the getXX and setXXX so we could reference the elements via eg, get(Entity1_.childObject).get(ChildObject_.grandChildObject) to assist in refactoring. 我们专门与getXX和setXXX一起使用,因此我们可以通过get(Entity1_.childObject).get(ChildObject_.grandChildObject)引用元素,以协助重构。 JPA also supports it via string name so you could implement the getPredicates with that. JPA还通过字符串名称支持它,因此您可以使用它来实现getPredicates。

JPA requires the actual Entity.class reference in their calls it was a bit of fun trying to obtain it. JPA在他们的调用中需要实际的Entity.class引用,尝试获取它很有趣。 Eventually a google search turned it up. 最终,谷歌搜索打开了它。

find fetches the row respect to the primary key.Now as you want " common find method which accepts a map having the where clause conditions. In the map, key will be the column name and value will be the column name value of the where clause. This method should return the list of selected rows" for this you have to go for CriteriaQuery like this : find获取相对于主键的行。现在,您要使用“ 通用find方法,该方法接受具有where子句条件的映射。在映射中,key将是列名,value将是where子句的列名值。此方法应返回“选定行的列表” ,为此您必须像下面这样去CriteriaQuery:

The following simple Criteria query returns all instances of the Pet entity in the data source: 以下简单的Criteria查询返回数据源中Pet实体的所有实例:

EntityManager em = ...;
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
Root<Pet> pet = cq.from(Pet.class);
cq.select(pet);
TypedQuery<Pet> q = em.createQuery(cq);
List<Pet> allPets = q.getResultList();

The equivalent JPQL query is: 等效的JPQL查询为:

SELECT p FROM Pet p

Moreover i will advise you to go for annotation based mapping in your entities & look for setter & getter for the methods.In that design also you can have customized method by java logic for this go here to read. 此外,我建议您在您的实体中进行基于注释的映射,并为方法寻找setter和getter。在该设计中,您还可以通过Java逻辑定制方法,以便在此处阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM