Java JPA 标准教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3997070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA Criteria Tutorial
提问by John Manak
I've been trying to find a JPA Criteria API tutorial but haven't been much successful. Do you know about any for beginners? I'd like to start using it in an Java5/Maven app to build complex search queries.
我一直在寻找 JPA Criteria API 教程,但没有取得多大成功。你知道有哪些适合初学者的吗?我想开始在 Java5/Maven 应用程序中使用它来构建复杂的搜索查询。
采纳答案by Pascal Thivent
The Dynamic, typesafe queries in JPA 2.0article is a very good one on this topic, actually the best one I've found so far online, even better than the Chapter 23 Using the Criteria API to Create Queriesfrom the Java EE 6 tutorials (that contains some mistakes).
JPA 2.0文章中的动态类型安全查询是关于该主题的一篇非常好的文章,实际上是我目前在网上找到的最好的文章,甚至比Java EE 6 教程中的第 23 章使用 Criteria API 来创建查询(包含一些错误)。
回答by Aaron Saunders
回答by John Manak
Pro JPA 2: Mastering the Java Persistence API http://books.google.com/books?id=j84hdeHH2PYC
专业 JPA 2:掌握 Java 持久性 API http://books.google.com/books?id=j84hdeHH2PYC
This is the source I find the most useful.
这是我认为最有用的来源。
回答by Marcin Szymczak
Examples of common queries are here
常见查询示例在这里
All examples are in this form:
所有示例均采用以下形式:
CriteriaBuilder cb = em.getCriteriaBuilder();
// Query for a List of objects.
CriteriaQuery cq = cb.createQuery();
Root e = cq.from(Employee.class);
cq.where(cb.greaterThan(e.get("salary"), 100000));
Query query = em.createQuery(cq);
List<Employee> result = query.getResultList();
If you are also considering other technologies you should seriously consider querydsl. Main advantages over criteria include shorter code, good readability and similar syntax to regular sql.
如果您还在考虑其他技术,您应该认真考虑querydsl。与标准相比的主要优势包括更短的代码、良好的可读性以及与常规 sql 相似的语法。
Example QueryDSL code here:
示例 QueryDSL 代码在这里:
JPAQuery query = new JPAQuery(entityManager);
List<Person> persons = query.from(person)
.where(
person.firstName.eq("John")),
.list(person);