简体   繁体   English

Java / JPA中类似Django的查询

[英]Django-like queries in Java/JPA

Is there any library or framework to make JPA queries in a less verbose way such as: 是否有任何库或框架以较简单的方式进行JPA查询,例如:

User.query("age < 30")

instead of: 代替:

Query query = entityManager.createQuery("select u FROM User u WHERE age < 30");
return query.getResultList();

I suppose there is no standard way to do it with JPA. 我想没有标准的方法来使用JPA。 I've seen Hibernate Criteria API , that is not as simple as Django and forces your application to be coupled to Hibernate. 我见过Hibernate Criteria API ,它不像Django那么简单,并强制你的应用程序与Hibernate耦合。 I'd like to know what do you think about it and other approaches. 我想知道你对它和其他方法的看法。

Yes, a Java framework allow you to do that: Play! 是的,Java框架允许您这样做:玩! framework. 框架。 It's a framework similar to Django but complety written using Java. 它是一个类似于Django的框架,但是使用Java编写的完整框架。 http://www.playframework.org/documentation/1.0.1/model http://www.playframework.org/documentation/1.0.1/model

For instance you can write something like: 例如,您可以编写如下内容:

User.find("age< ?", 30).fetch();

Moreover this is not coupled to hibernate you can use other technologies like Google App Engine datastore or Amazon Simple DB throw Siena 此外,这与hibernate无法结合,您可以使用其他技术,如Google App Engine数据存储区或Amazon Simple DB抛出Siena

You can make your query shorter: 您可以缩短查询时间:

from User where age < 30

Additionally I would like to add that the Hibernate API is much more powerful and adds things like polymorphism and prefetching in a nice way, so don't give up on it yet. 另外我想补充一点,Hibernate API功能更强大,并且以一种很好的方式添加了多态和预取等功能,所以不要放弃它。

与此相关,我还发现Spring Roo能够生成遵循此“Active Record Pattern”的实体,并允许开发人员包含finder方法。

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

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