Save
/ ** * Save an entity * / @Test public void testAdd () { // Define object Customer c = new Customer (); c.setCustName ( "Chuanzhi Academy" ); c.setCustLevel ( "VIP customer" ); c.setCustSource ( "Network" ); c.setCustIndustry ( "IT Education" ); c.setCustAddress ( "Changping District Beiqijia Town" ); c.setCustPhone ( " 010-84389340 " ); EntityManager em = null ; EntityTransaction tx =null ; try { // Get entity management object em = JPAUtil.getEntityManager (); // Get transaction object tx = em.getTransaction (); // Start transaction tx.begin (); // Perform operation em.persist (c ); // commit transaction tx.commit (); } catch (Exception e) { // rollback transaction tx.rollback (); e.printStackTrace (); } finally { //Release resources em.close (); } }
modify
@Test public void testMerge () { // Define object EntityManager em = null ; EntityTransaction tx = null ; try { // Get entity management object em = JPAUtil.getEntityManager (); // Get transaction object tx = em.getTransaction () ; // Start transaction tx.begin (); // Perform operation Customer c1 = em.find (Customer. Class , 6L ); c1.setCustName ("Jiangsu Chuanzhi Institute" ); em.clear (); // Clear the c1 object from the cache em.merge (c1); // Commit transaction tx.commit (); } catch (Exception e) { // Rollback transaction tx.rollback (); e.printStackTrace (); } finally { // Release resources em.close (); } }
delete
/ ** * Delete * / @Test public void testRemove () { // Define object EntityManager em = null ; EntityTransaction tx = null ; try { // Get entity management object em = JPAUtil.getEntityManager (); // Get transaction object tx = em.getTransaction (); // Start transaction tx.begin (); // Perform operation Customer c1 = em.find (Customer. class , 6L ); em.remove (c1); // commit transaction tx.commit (); } catch (Exception e) { // rollback transaction tx.rollback (); e.printStackTrace (); } finally { // release resource em. close (); } }
Query by Id
/ ** * Query one: Use the strategy of immediate loading * / @Test public void testGetOne () { // Define object EntityManager em = null ; EntityTransaction tx = null ; try { // Get entity management object em = JPAUtil.getEntityManager ( ); // Get transaction object tx = em.getTransaction (); // Start transaction tx.begin (); // Perform operation Customer c1 = em.find (Customer. Class , 1L); // commit transaction tx.commit (); System.out.println (c1); // output query object } catch (Exception e) { // rollback transaction tx.rollback (); e.printStackTrace (); } finally { // Release resources em.close (); } } // Query the cache problem of the entity @Test public void testGetOne () { // Define the object EntityManager em = null ; EntityTransaction tx= null ; try { // Get entity management object em = JPAUtil.getEntityManager (); // Get transaction object tx = em.getTransaction (); // Start transaction tx.begin (); // Execute operation Customer c1 = em .find (Customer. class , 1L ); Customer c2 = em.find (Customer. class , 1L ); System.out.println (c1 == c2); // The output result is true, EntityManager also has a cache // Commit transactions tx.commit (); System.out.println (c1); } catch (Exception e) { // rollback transaction tx.rollback (); e.printStackTrace (); } finally { // release resource em.close ( ); } } // Lazy loading strategy method: / ** * Query one: Use lazy loading strategy * / @Test public void testLoadOne () { // Define object EntityManager em = null ; EntityTransaction tx = null; try { // Get entity management object em = JPAUtil.getEntityManager (); // Get transaction object tx = em.getTransaction (); // Start transaction tx.begin (); // Execute operation Customer c1 = em.getReference (Customer. Class , 1L ); // commit transaction tx.commit (); System.out.println (c1); } catch (Exception e) { // rollback transaction tx.rollback (); e.printStackTrace (); }finally { // Release resources em.close (); } }