Using JPA CRUD operations completed

Basic CRUD operations as follows:

  1 package cn.itheima.test;
  2 
  3 import cn.itcast.domain.Customer;
  4 import cn.itcast.utils.JpaUtils;
  5 import org.junit.Test;
  6 
  7 import javax.persistence.EntityManager;
  8 import javax.persistence.EntityManagerFactory;
  9 import javax.persistence.EntityTransaction;
 10 import javax.persistence.Persistence;
 11 
 12 public class JpaTest {
 13 
 14     /**
 15      * 测试jpa的保存
 16      * Case: save a client to the database
 17       * JPA steps
 18       * 1. Load factory profile creation (the entity manager factory) objects
 19       * 2. The entity manager factory manager by the entity acquired
 20       * 3. Get affairs objects, open transaction
 21       * 4. complete CRUD operations
 22       * 5. commit the transaction (transaction rollback)
 23       * 6. release resources
 24       * / 
25      @Test
 26 is      public  void testSave () {
 27  //         // . 1. load profile creation factory (the entity manager factory) objects
 28  //         the EntityManagerFactory = factory's Persistence.createEntityManagerFactory ( "myJpa");
 29  //         // 2. acquiring entity manager entity Manager factory
 30  //        EM = factory.createEntityManager the EntityManager (); 
31 is          the EntityManager EM = JpaUtils.getEntityManager ();
 32          // 3. acquisition transaction object, the transaction opening 
33 is          the EntityTransaction em.getTransaction TX = (); // Get the transaction object 
34 is          tx.begin ( ); // open transaction
 35          // 4. complete CRUD operations: a client to save the database 
36          the customer = customer new new the customer ();
 37 [          customer.setCustName ( "Chi Chuan podcast" );
 38 is          customer.setCustIndustry ( " Education " );
 39          // save, 
40          EM.the persist (Customer); // save operation
 41          // 5 commit the transaction 
42 is          tx.commit ();
 43 is          // 6. The release resources 
44 is          em.close ();
 45          //        factory.close (); 
46 is  
47      }
 48  
49      / ** 
50       * id according to customer queries
 51       * use the find method to query:
 52       * 1. the object query itself is the current target customers
 53       * 2. when calling find methods to query the database sql statement will be sent
 54       *
 55       * Now loading
 56       *
 57       *
 58       * /
59      @Test
 60      public   void testFind () {
 61 is          // 1. acquired by the tools entityManager 
62 is          the EntityManager entityManager = JpaUtils.getEntityManager ();
 63 is          // 2. transaction opening 
64          the EntityTransaction TX = entityManager.getTransaction ();
 65          TX. the begin ();
 66          // 3. CRUD - the customer id query 
67          / ** 
68           * Find: the id query data
 69           * class: results of a query of data types to be packaged entity class bytecode
 70           * id: primary key value query
 71           * / 
72         Customer = entityManager the Customer. Find (the Customer. Class , 1L );
 73 is          // of System.out.print (Customer);
 74          // 4. commit the transaction 
75          tx.commit ();
 76          // 5. The release resources 
77          entityManager. Close ();
 78      }
 79  
80      / ** 
81       * the customer id query
 82       * getReference method
 83       * 1. Get the object is a dynamic proxy object
 84       * 2. the method does not immediately send call getReference sql statement queries the database
 85       * * when calling query result object when the statement will be sent sql query: when to use, when to send sql statement to query the database
86       *
 87       * delay loading (lazy loading)
 88       * * obtained is a dynamic proxy object
 89       * * when used, the use of what will query
 90       * / 
91 is      @Test
 92      public   void testReference () {
 93          // . 1. acquired by the tools entityManager 
94          the entityManager entityManager = JpaUtils.getEntityManager ();
 95          // 2. transaction opening 
96          the EntityTransaction TX = entityManager.getTransaction ();
 97          tx.begin ();
 98          // 3. CRUD - The id customer inquiries 
99          / ** 
100          * GetReference: query data based on the id
 101           * class: results of a query of data types to be packaged entity class bytecode
 102           * id: primary key value query
 103           * / 
104          the Customer = Customer entityManager. GetReference (the Customer. Class , 1L );
 105          of System.out.print (Customer);
 106          // 4. commit the transaction 
107          tx.commit ();
 108          // 5. The release resources 
109          entityManager.close ();
 110      }
 111  
112      / ** 
113       * delete customer case
 114       *
 115       * /
1 16      @Test
 117      public   void testRemove () {
 1 18          // 1. entityManager acquired by tools 
119          the EntityManager entityManager = JpaUtils.getEntityManager ();
 120          // 2. Open Services 
121          the EntityTransaction TX = entityManager.getTransaction ();
 122          TX. the begin ();
 123          // 3. CRUD - delete client
 124  
125          // I The customer id query 
126          the customer = customer EntityManager.find (the customer. class , 1L );
 127          // II remove method call to complete the deletion 
128         . entityManager Remove (Customer);
 129  
130.          // 4. commit the transaction 
131 is          tx.commit ();
 132          // 5. The release resources 
133          entityManager.close ();
 134      }
 135  
136      / ** 
137       operate to update the customer *
 138       Merge * (Object)
 139       * / 
140      @Test
 141 is      public   void testUpdate () {
 142          // 1. acquired by the tools entityManager 
143          the entityManager entityManager = JpaUtils.getEntityManager ();
 144          //2. Turn on the transaction 
145          the EntityTransaction tx = entityManager.getTransaction ();
 146          tx.begin ();
 147          // 3. CRUD - update operations
 148  
149          // i query client 
150          the Customer the Customer = EntityManager.find (the Customer. class , 1L );
 151          // II update client 
152          customer.setCustIndustry ( "IT education" );
 153          entityManager. Merge (customer);
 154  
155          // 4. commit the transaction 
156          tx.commit ();
 157          // . 5. release resources 
158         entityManager.close();
159     }
160 
161 }

Description: find () and getReference () the difference:

According to id customer queries 
    using the find method to query: 
    1. object query object itself is the current client 
    2. When calling find methods to query the database sql statement will be sent 
immediately loaded 
============= ======================================== 
according to customer queries id getReference method 
         1. acquired object is a dynamic proxy object 
         2. call getReference method does not immediately send sql statement to query the database 
             * when calling query result object when sql statement will send a query: when to use, when to send sql statement to query the database 
lazy loading ( lazy loading) 
     * get is a dynamic proxy object 
     * when to use, what use would query
    

 

Guess you like

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