07 simple queries

JPQL full name of the Java Persistence Query Language
-based EJB Query Language (EJB QL) was first introduced in the EJB2.0, Java Persistence Query Language (JPQL) is a portable query language designed to express object-oriented expression language type, the SQL syntax and semantics of simple queries using query-bound 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 property access, instead of the table and the property name table.

All inquiries

// query for all customers 
    @Test
     public  void findAll () {
        EntityManager em = null;
        TX the EntityTransaction = null ;
         the try {
             // Get entity management object 
            EM = JPAUtil.getEntityManager ();
             // get a transaction objects 
            TX = em.getTransaction ();
            tx.begin();
            // Create a query object 
            String = JPQL "from the Customer" ;
            Query Query = em.createQuery (JPQL);
             // query and returns the obtained result 
            List List query.getResultList = (); // get a set of return type 
            for (Object Object: List) {
                System.out.println(object);
            }
            tx.commit();
        } The catch (Exception E) {
             // Rollback transaction 
            tx.rollback ();
            e.printStackTrace ();
        } The finally {
             // release resources 
            em.close ();
        }
    }

Paging query

// paging query customers 
    @Test
     public  void findPaged () {
        EntityManager em = null;
        TX the EntityTransaction = null ;
         the try {
             // Get entity management object 
            EM = JPAUtil.getEntityManager ();
             // get a transaction objects 
            TX = em.getTransaction ();
            tx.begin();

            // Create a query object 
            String = JPQL "from the Customer" ;
            Query Query = em.createQuery (JPQL);
             // start index 
            query.setFirstResult (0 );
             // page number display bar 
            query.setMaxResults (10 );
             // query and returns the obtained result 
            List list = query.getResultList ( ); // get a set of return type 
            for (Object Object: List) {
                System.out.println(object);
            }
            tx.commit();
        } The catch (Exception E) {
             // Rollback transaction 
            tx.rollback ();
            e.printStackTrace ();
        } The finally {
             // release resources 
            em.close ();
        }
    }

Conditions inquiry

// condition query 
    @Test
     public  void findCondition () {
        EntityManager em = null;
        TX the EntityTransaction = null ;
         the try {
             // Get entity management object 
            EM = JPAUtil.getEntityManager ();
             // get a transaction objects 
            TX = em.getTransaction ();
            tx.begin();
            //创建query对象
            String jpql = "from Customer where cName like ?1 ";
            Query Query = em.createQuery (JPQL);
             // for placeholder assignment from the start. 1 
            query.setParameter (. 1, "% Zhao%" );
             // query and returns the results to give 
            Object object = query.getSingleResult (); // get only result set object 
            System.out.println (object);
            tx.commit();
        } The catch (Exception E) {
             // Rollback transaction 
            tx.rollback ();
            e.printStackTrace ();
        } The finally {
             // release resources 
            em.close ();
        }
    }

Sort query

// customer id reverse query all customers
     // query for all customers 
    @Test
     public  void findOrder () {
        EntityManager em = null;
        TX the EntityTransaction = null ;
         the try {
             // Get entity management object 
            EM = JPAUtil.getEntityManager ();
             // get a transaction objects 
            TX = em.getTransaction ();
            tx.begin();
            // Create a query object 
            String = JPQL "from the Order by the Customer cid desc" ;
            Query Query = em.createQuery (JPQL);
             // query and returns the obtained result 
            List List query.getResultList = (); // get a set of return type 
            for (Object Object: List) {
                System.out.println(object);
            }
            tx.commit();
        } The catch (Exception E) {
             // Rollback transaction 
            tx.rollback ();
            e.printStackTrace ();
        } The finally {
             // release resources 
            em.close ();
        }
    }

D

// statistical inquiry 
    @Test
     public  void findCount () {
        EntityManager em = null;
        TX the EntityTransaction = null ;
         the try {
             // Get entity management object 
            EM = JPAUtil.getEntityManager ();
             // get a transaction objects 
            TX = em.getTransaction ();
            tx.begin();
            // query all client
             // 1. Create a query object 
            String = JPQL "the SELECT COUNT (cid) from the Customer" ;
            Query Query = em.createQuery (JPQL);
             // 2. query and returns the results to give 
            Object query.getSingleResult = COUNT (); // get a set of return type 
            System.out.println (count);
            tx.commit();
        } The catch (Exception E) {
             // Rollback transaction 
            tx.rollback ();
            e.printStackTrace ();
        } The finally {
             // release resources 
            em.close ();
        }
    }

 

Guess you like

Origin www.cnblogs.com/zhaochengf/p/12127590.html
07