Hibernate notes 7--JPA CRUD, 7 -- jpacrud
1. Set up the environment, pay attention to the problem of the package structure, src to create a folder named META-INF, put persistence. xml, the location is put wrong, will report an error if not read.
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <persistence xmlns = "http://java.sun.com/xml/ns/persistence" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://java.sun.com/xml/ns/persistence 5 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 6 version = "2.0"> 7 8 <! -- Configure the persistence unit --> 9 <persistence-unit name = "unit" transaction-type = "RESOURCE_LOCAL"> 10 <! -- Configure the jpa persistence class provider --> 11 <provider> org. hibernate. jpa. HibernatePersistenceProvider </provider> 12 <! -- Configure the basic information of the database Hibernate --> 13 <properties> 14 <property name = "hibernate. connection. driver_class "value =" com. mysql. jdbc. driver "/> 15 <property name =" hibernate. connection. url "value =" jdbc: mysql: // day581 "/> 16 <property name =" hibernate. connection. username "value =" root "/> 17 <property name =" hibernate. connection. password "value =" root "/> 18 <property name =" hibernate. dialect "value =" org. hibernate. dialect. MySQLDialect "/> 19 <property name =" hibernate. hbm2ddl. auto "value =" update "/> 20 <property name =" hibernate. show_ SQL "value =" true "/> 21 <property name =" hibernate. format_ SQL "value =" true "/> 22 <property name =" hibernate. connection. isolation "value =" 4 "/> 23 <! -- C3p0 connection pool --> 24 <property name = "hibernate. connection. provider_class "value =" org. hibernate. connection. c3P0ConnectionProvider "/> 25 </properties> 26 </persistence-unit> 27 </persistence>
2. Compiling object classes
1 package huguangqin.com.cnblogs.entity; 2 import java.io.Serializable; 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.GenerationType; 7 import javax.persistence.Id; 8 import javax.persistence.Table; 9 10 @Entity 11 @Table(name = "cst_customer") 12 public class Customer implements Serializable { 13 private static final long serialVersionUID = 1L; 14 15 @Id 16 @GeneratedValue(strategy = GenerationType.IDENTITY) 17 @Column(name = "cust_id") 18 private Long custId; 19 20 @Column(name = "cust_name") 21 private String custName; 22 23 @Column(name = "cust_source") 24 private String custSource; 25 26 @Column(name = "cust_industry") 27 private String custIndustry; 28 29 @Column(name = "cust_level") 30 private String custLevel; 31 32 @Column(name = "cust_address") 33 private String custAddress; 34 35 @Column(name = "cust_phone") 36 private String custPhone; 37 38 public Long getCustId() { 39 return custId; 40 } 41 42 public void setCustId(Long custId) { 43 this.custId = custId; 44 } 45 46 public String getCustName() { 47 return custName; 48 } 49 50 public void setCustName(String custName) { 51 this.custName = custName; 52 } 53 54 public String getCustSource() { 55 return custSource; 56 } 57 58 public void setCustSource(String custSource) { 59 this.custSource = custSource; 60 } 61 62 public String getCustIndustry() { 63 return custIndustry; 64 } 65 66 public void setCustIndustry(String custIndustry) { 67 this.custIndustry = custIndustry; 68 } 69 70 public String getCustLevel() { 71 return custLevel; 72 } 73 74 public void setCustLevel(String custLevel) { 75 this.custLevel = custLevel; 76 } 77 78 public String getCustAddress() { 79 return custAddress; 80 } 81 82 public void setCustAddress(String custAddress) { 83 this.custAddress = custAddress; 84 } 85 86 public String getCustPhone() { 87 return custPhone; 88 } 89 90 public void setCustPhone(String custPhone) { 91 this.custPhone = custPhone; 92 } 93 94 // toString 95 @Override 96 public String toString() { 97 return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource 98 + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress 99 + ", custPhone=" + custPhone + "]";100 }101 102 }103
3. Tool class-JPAUtil, used to start creating EntityManagerFactory once and obtain EntityManager
1 package huguangqin.com.cnblogs.util; 2 import javax.persistence.EntityManager; 3 import javax.persistence.EntityManagerFactory; 4 import javax.persistence.Persistence; 5 6 public class JPAUtil { 7 private static EntityManagerFactory factory; 8 9 static { 10 factory = Persistence.createEntityManagerFactory("unit"); 11 } 12 13 public static EntityManager getEntityManager() { 14 return factory.createEntityManager(); 15 } 16 } 17
4. Test code
1 package huguangqin.com. cnblogs. demo; 2 3 import javax. persistence. entityManager; 4 import javax. persistence. entityTransaction; 5 6 import org. junit. test; 7 8 import huguangqin.com. cnblogs. entity. customer; 9 import huguangqin.com. cnblogs. util. JPAUtil; 10 11/** 12 * save a data entry to the customer table: persist 13 */14 public class JPATest1 {15 @ Test 16 public void addTest () {17 // obtain EntityManager 18 EntityManager em = JPAUtil. getEntityManager (); 19 // get the transaction object 20 EntityTransaction et = em. getTransaction (); 21 // start transaction 22 et. begin (); 23 // CRUD 24 Customer cust = new Customer (); 25 cust. setCustName ("Ali"); 26 // persistence 27 em. persist (cust); 28 // submit transaction 29 et. commit (); 30 // close resource 31 em. close (); 32} 33 34/** 35 * query by id: find 36 */37 @ Test 38 public void findTest () {39 // get EntityManager 40 EntityManager em = JPAUtil. getEntityManager (); 41 // obtain the transaction object 42 EntityTransaction et = em. getTransaction (); 43 // enable transaction 44 et. begin (); 45 // query 46 Customer customer = em by ID. find (Customer. class, 1L); 47 System. out. println (customer); 48 49 // submit transaction 50 et. commit (); 51 // close resource 52 em. close (); 53} 54 55/** 56 * getReference (latency load Test), send SQL 57 */58 @ Test 59 public void delayTest () only when used () {60 // obtain EntityManager 61 EntityManager em = JPAUtil. getEntityManager (); 62 // get the transaction object 63 EntityTransaction et = em. getTransaction (); 64 // enable transaction 65 et. begin (); 66 // delay loading 67 Customer cust = em. getReference (Customer. class, 1L); 68 System. out. println (cust); 69 // submit transaction 70 et. commit (); 71 // close the resource 72 em. close (); 73} 74 75/** 76 * modification: Query-Modify (setter) 77 */78 @ Test 79 public void updateTest () {80 // obtain EntityManager 81 EntityManager em = JPAUtil. getEntityManager (); 82 // obtain the transaction object 83 EntityTransaction et = em. getTransaction (); 84 // start transaction 85 et. begin (); 86 // modify 87 Customer cust = em. find (Customer. class, 1L); 88 cust. setCustName ("Tecent"); 89 90 // submit transaction 91 et. commit (); 92 // closes the resource 93 em. close (); 94} 95 96/** 97 * Delete remove 98 */99 @ Test100 public void delTest () {101 // get EntityManager102 EntityManager em = JPAUtil. getEntityManager (); 103 // obtain the transaction object 104 EntityTransaction et = em. getTransaction (); 105 // start the transaction 106 et. begin (); 107 // Delete the 108 Customer cust = em. find (Customer. class, 1L); 109 em. remove (cust); 110 // submit the transaction 111 et. commit (); 112 // close resource 113 em. close (); 114} 115} 116