1、2008-03-11OpenJPA关键字:jpa6Query6.1JPQLQueries6.1.1QueryBasicsJava代码1.public Query createQuery(String jpql); publicQuerycreateQuery(Stringjpql); EntityManager上的这个方法用来根据给定的JPQL创建Query。Java代码1.public List getResultList(); publicListgetResultList(); Query上的这个方法用来得到query的执行结果,以下是个简单的例子:Java代码1.En
2、tityManager em = entityManagerFactory.createEntityManager(); 2.Query q = em.createQuery("SELECT x FROM Magazine x"); 3.List results = (List) q.getResultList(); 4.em.close(); EntityManagerem=entityManagerFactory.createEntityManager();Queryq=em.createQuery("SELECTxFROMMaga
3、zinex");Listresults=(List)q.getResultList();em.close(); JPQLquery的from子句中定义了query内部命名空间。可以将任意的标识符赋值给entities,之后就可以在query的任意位置上通过标识符引用entities。在from子句中,关键字as是可选的。例如以下两个JPQL等效:Sql代码1.SELECT x FROM Magazine x 2.SELECT x FROM Magazine AS x SELECTxFROMMagazinexSELECTxFROMMaga
4、zineASx 当查询entities的时候,关键字object也是可选的,例如selectx和SELECTOBJECT(x)是等效的。JPQL中的关键字不是大小写敏感的,而entity、identifier和membernames是大小写敏感的。以下是一些JPQL的例子: Sql代码1.SELECT x FROM Magazine x WHERE x.TITLE = 'JDJ' 2.SELECT x FROM Magazine x WHERE x.title = 'JDJ' OR x.title = 'JavaPro' 3.SELECT x FROM Magazine x W
5、HERE x.price > 3.00 AND x.price <= 5.00 4.SELECT x FROM Magazine x WHERE x.price <> 3.00 5.SELECT x FROM Magazine x WHERE (x.price > 3.00 AND x.price <= 5.00) OR x.price = 7.00 1.SELECT x FROM Magazine x WHERE x.price > 3.00 AND (x.price <= 5.00 OR x.price = 7.00) 2.SELECT x FROM Magazine x
6、WHERE x.price >= 3.00 AND x.price <= 5.00 3.SELECT x FROM Magazine x WHERE x.price BETWEEN 3.00 AND 5.00 4.SELECT x FROM Magazine x WHERE x.title LIKE 'J%' 5.SELECT x FROM Magazine x WHERE x.title LIKE 'J__' 6.SELECT x FROM Magazine x WHERE x.title IN ('JDJ', 'JavaPro', 'IT Insider') 7.SELE
7、CT x FROM Magazine x WHERE x.articles is empty 8.SELECT x FROM Magazine x WHERE x.publisher is null 1.SELECT x FROM Magazine x WHERE NOT(x.price = 10.0) SELECTxFROMMagazinexWHEREx.TITLE='JDJ'SELECTxFROMMagazinexWHEREx.tit