JPA - FindByExample [英] JPA - FindByExample

查看:173
本文介绍了JPA - FindByExample的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个很好的例子来说明如何在JPA中执行findByExample,它可以通过任何实体类型的反射在泛型DAO中工作?我知道我可以通过我的提供商(Hibernate)做到这一点,但我不想打破中立......

Does anyone have a good example for how to do a findByExample in JPA that will work within a generic DAO via reflection for any entity type? I know I can do it via my provider (Hibernate), but I don't want to break with neutrality...

看起来像标准API可能就是这样去....但我不知道如何处理它的反射部分。

Seems like the criteria API might be the way to go....but I am not sure how to handle the reflection part of it.

推荐答案

实际上,按示例查询(QBE)已被考虑包含在JPA 2.0规范中,但不包括在内,即使主要供应商支持它也是如此。引用Mike Keith:

Actually, Query By Example (QBE) has been considered for inclusion in the JPA 2.0 specification but is not included, even if major vendors support it. Quoting Mike Keith:


我很遗憾地说我们实际上没有在JPA 2.0中做QBE。 Criteria API没有任何特殊的运算符,因此实体相等就像JP QL一样,基于PK值。对不起,但希望我们在下一轮比赛中能在这方面取得更大的成功。目前,它是每个供应商支持的供应商功能之一,但尚未在规范中。

I'm sorry to say that we didn't actually get to do QBE in JPA 2.0. Criteria API does not have any special operators for it so entity equality is just like in JP QL, based on PK value. Sorry, but hopefully we'll be more successful on that front in the next go-round. For now it is one of those vendor features that every vendor supports, but is not in the spec yet.

为了以防万一,我为下面的主要供应商添加了(非通用的)示例代码,用于文档目的。

Just in case, I've added (non generic) sample code for the major vendors below for documentation purposes.

以下是在EclipseLink JPA 2.0参考实现中使用QBE的示例:

Here is a sample of using QBE in the EclipseLink JPA 2.0 reference implementation:

// Create a native EclipseLink query using QBE policy
QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.excludeDefaultPrimitiveValues();
ReadObjectQuery q = new ReadObjectQuery(sampleEmployee, policy);

// Wrap the native query in a standard JPA Query and execute it 
Query query = JpaHelper.createQuery(q, em); 
return query.getSingleResult(); 



OpenJPA



OpenJPA支持这种风格查询通过其扩展的 OpenJPAQueryBuilder 界面:

CriteriaQuery<Employee> q = cb.createQuery(Employee.class);

Employee example = new Employee();
example.setSalary(10000);
example.setRating(1);

q.where(cb.qbe(q.from(Employee.class), example);



Hibernate



使用Hibernate的Criteria API:

Hibernate

And with Hibernate's Criteria API:

// get the native hibernate session
Session session = (Session) getEntityManager().getDelegate();
// create an example from our customer, exclude all zero valued numeric properties 
Example customerExample = Example.create(customer).excludeZeroes();
// create criteria based on the customer example
Criteria criteria = session.createCriteria(Customer.class).add(customerExample);
// perform the query
criteria.list();

现在,应该可以通过JPA 2.0 Criteria API和反射以供应商中立的方式实现一些接近的东西,我真的很想知道它是否值得付出努力。我的意思是,如果您将上述任何代码片段设为通用并将代码放在DAO方法中,从一个供应商切换到另一个供应商将非常容易如果需要应该出现。我同意这不理想,但仍然。

Now, while it should be possible to implement something approaching in a vendor neutral way with JPA 2.0 Criteria API and reflection, I really wonder if it's worth the effort. I mean, if you make any of the above snippets generic and put the code in a DAO method, it would be quite easy to switch from one vendor to another if the need should arise. I agree it's not ideal, but still.

  • What about findByExample in JPA book?
  • Dynamic, typesafe queries in JPA 2.0

这篇关于JPA - FindByExample的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆