JPA in a complex query

JPQL全称Java Persistence Query Language

Based on EJB Query Language (EJB QL) was first introduced in the EJB2.0, Java Persistence Query Language (JPQL) is a portable query language designed object-oriented expression language of expression, and the SQL syntax · simple query semantic bound together using a query written in this language is portable and can be compiled on all major SQL database server.

Characterized in native SQL statements and the like, and a fully object-oriented, by the class name and attribute access, rather than the table name and the attribute table .

1, All inquiries

1      / * 
2       * All queries
 . 3       * jqpl: from cn.itcast.domain.Customer
 . 4       * SQL: the FROM cst_customer the SELECT *
 . 5       * / 
. 6      @Test
 . 7      public  void testFindAll () {
 . 8          // 1. Get the object entityManager 
9          EM = the EntityManager JpaUtils.getEntityManager ();
 10          // 2. open transaction 
. 11          the EntityTransaction TX = em.getTransaction ();
 12 is          tx.begin ();
 13 is          // 3. query all
 14         // Customer equivalent from cn.itcast.domain.Customer, the package name is omitted here, Customer can not be written in the Customer
 15          String JPQL = "from the Customer";
 16          Query Query = em.createQuery (JPQL); // Create a query Query objects, query execution object is the object of jqpl
 . 17  
18 is          // send a query, the result set and packaged 
. 19          List List query.getResultList = ();
 20 is  
21 is          for (Object obj: List) {
 22 is              of System.out.print (obj) ;
 23          }
 24  
25          // 4 commit the transaction 
26 is          tx.commit ();
 27          // 5. The release resources 
28          em.close ();
 29      }

2, ordering inquiry

1      / * 
2       * sort query: reverse lookup of all customers (The reverse id)
 . 3       * SQL: the SELECT * the FROM cst_customer the cust_id the ORDER BY DESC
 . 4       * JPQL: from the Customer Order by custId desc
 . 5       * (custId: entity class attributes)
 6       *
 7       * query for jpql
 8       * 1. Create query query object
 9       * parameterizing 2.
 10       * 3. query, and returns the obtained result
 . 11       * / 
12 is      @Test
 13 is      public  void testOrders () {
 14          // . 1. Gets the object entityManager 
15          the entityManager EM =JpaUtils.getEntityManager ();
 16          // 2. affairs turned 
17          the EntityTransaction tx = em.getTransaction ();
 18          tx.begin ();
 19          // 3. All inquiries 
20          String JPQL = "from the Order by the Customer custId desc";
 21 is          query query = em.createQuery (JPQL); // create a query object query, query objects is performed jqpl object
 22 is  
23 is          // send a query, the result set and the package 
24          List List query.getResultList = ();
 25  
26 is          for (Object obj: List) {
 27              System.out.println (obj);
 28          }
 29  
30          //4. commit the transaction 
31 is          tx.commit ();
 32          // 5. The release resources 
33 is          em.close ();
 34 is      }

3, statistical inquiry

1      / * 
2       * JPQL use queries, aggregate statistics of customers
 . 3       * SQL: the SELECT COUNT (the cust_id) the FROM cst_customer
 . 4       * JPQL: SELECT COUNT (custId) from the Customer
 . 5       * / 
. 6      @Test
 . 7      public  void testCount () {
 . 8          // 1. obtain entityManager objects 
. 9          the entityManager EM = JpaUtils.getEntityManager ();
 10          // 2. open transaction 
. 11          the EntityTransaction TX = em.getTransaction ();
 12 is          tx.begin ();
 13 is          // 3. query all
 14          / / i. query query object is created according to the statement jpql
15          String JPQL = "SELECT COUNT (custId) from the Customer";
 16          Query Query = em.createQuery (JPQL);
 . 17          // . II assignment parameter
 18 is          // . Contact III, and the results of the package 
. 19  
20 is          / ** 
21           * getResultList: query result list is set directly to the package
 22 is           * the getSingleResult: obtain a unique result set
 23 is           * / 
24          Object result = query.getSingleResult ();
 25  
26 is          System.out.println (result);
 27  
28          // . 4. commit the transaction 
29          tx.commit ();
 30          // 5. release resources 
31          em.close ();
32     }

4, paging query

1      / * 
2       * paging query
 . 3       * SQL: SELECT * from limit cst_customer 0,2
 . 4       * jqpl: from the Customer
 . 5       * / 
. 6      @Test
 . 7      public  void testPaged () {
 . 8          // 1. entityManager acquisition target 
. 9          the EntityManager EM = JpaUtils.getEntityManager ();
 10          // 2. affairs turned 
11          the EntityTransaction tx = em.getTransaction ();
 12          tx.begin ();
 13          // 3. all inquiries
 14          // i create a query object query jpql according to the statement. 
15         JPQL = String "from the Customer";
 16          Query Query = em.createQuery (JPQL);
 . 17          // II assignment parameters - paging parameters.
 18          // start index (from 0 starts here, does not include 0) 
. 19          Query .setFirstResult (0);
 20 is          // query page number of 
21 is          query.setMaxResults (2);
 22 is  
23 is          // III sending a query, and the result package. 
24  
25          / ** 
26 is           * getResultList: encapsulating query results directly list set
 27           * the getSingleResult: result set obtained only
 28           * / 
29          List query.getResultList list = ();
 30  
31 is          for (Object obj: list) {
 32              System.out.println (obj);
33          }
 34 is  
35          @ 4 to commit the transaction 
36          tx.commit ();
 37 [          // 5. The release resources 
38 is          em.close ();
 39      }

5, condition query

1      / ** 
2       * Ad Hoc Query
 3       * Case: Query client client name to 'Chi Chuan podcast' at the beginning of the
 4       * SQL:? The SELECT * the FROM cst_customer the WHERE CUST_NAME the LIKE
 5       * JPQL:? From the Customer the WHERE custName like
 6       * / 
7      @Test
 . 8      public  void TestCondition () {
 . 9          // 1. Get the object entityManager 
10          the entityManager EM = JpaUtils.getEntityManager ();
 . 11          // 2. transaction opening 
12 is          the EntityTransaction TX = em.getTransaction ();
 13 is          tx.begin () ;
 14          //3. Query All
 15          // i create a query object Query jpql according to the statement. 
16          String jpql =; "from the Customer the WHERE custName like?"
 17          Query Query = em.createQuery (jpql);
 18          // ii parameter assignment -. placeholder parameters
 19          // first parameter: placeholder index position (starting from 1), the second argument: value 
20 is          query.setParameter (1, "mass% Chi podcast");
 21 is  
22 is          // . iii sends a query result and the package 
23 is  
24          / ** 
25           * getResultList: a direct result of the query is a list set package
 26 is           * the getSingleResult: result set obtained only
 27           * / 
28          List query.getResultList list = ();
 29  
30          for (Object obj: list) {
31 is              System.out.println (obj);
 32          }
 33 is  
34 is          // 4. commit the transaction 
35          tx.commit ();
 36          // 5. The release resources 
37 [          em.close ();
 38 is      }

 

Guess you like

Origin www.cnblogs.com/116970u/p/11590468.html