1. Dependent jar package:
2. configuration file:
<? XML version = "1.0" encoding = "UTF-8" ?> < Persistence Xmlns = Http://java.sun.com/xml/ns/persistence" Xmlns : Xsi = Http://www.w3.org/2001/XMLSchema-instance" Xsi : Schemalocation =Http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" Version = "1.0" > < Persistence - Unit Name = "Hellojpapu" Transaction - Type = "Resource_local" > < Provider > Org. hibernate. EJB. hibernatepersistence </ Provider > < Properties > < Property Name = "Hibernate. dialect" Value = "Org. hibernate. dialect. mysql5dialect" /> < Property Name = "Hibernate. Connection. driver_class" Value = "Com. MySQL. JDBC. Driver" /> < Property Name = "Hibernate. Connection. url" Value = "JDBC: mysql: // localhost: 3306/JPA? Useunicode = true & amp; characterencoding = UTF-8" /> < Property Name = "Hibernate. Connection. username" Value = "Root" /> < Property Name = "Hibernate. max_fetch_depth" Value = "3" /> < Property Name = "Hibernate. hbm2ddl. Auto" Value = "Update" /> </ Properties > </ Persistence -Unit > </ Persistence >
3. Define the object Bean:
Import Java. util. date;Import Javax. Persistence. Basic; Import Javax. Persistence. column; Import Javax. Persistence. entity; Import Javax. Persistence. enumtype; Import Javax. Persistence. enumerated; Import Javax. Persistence. fetchtype; Import Javax. Persistence. generatedvalue; Import Javax. Persistence. generationtype; Import Javax. Persistence. ID; Import Javax. Persistence. lob;Import Javax. Persistence. Table; Import Javax. Persistence. Temporal; Import Javax. Persistence. temporaltype; Import Javax. Persistence. Transient; @ entity Public Class Person { Private Integer ID; Private String name; Private Date birthday; // 1988-02-01 Private Gender gender = gender. Man;Private String Info; Private Byte [] file; Private String imagepathstring; Public Person (){} Public Person (string name ){ This . Name = Name ;}@ ID @ generatedvalue (Strategy = generationtype. Auto) Public Integer GETID (){ Return ID ;} Public Void Setid (integer ID ){ This . ID = ID;} @ column (length = 10, nullable =False ) Public String getname (){ Return Name ;} Public Void Setname (string name ){ This . Name = Name ;}@ temporal (temporaltype. Date) Public Date getbirthday (){ Return Birthday ;} Public Void Setbirthday (date birthday ){ This . Birthday = birthday;} @ Enumerated (enumtype. String)// Save the index value to the database @ Column (length = 5, nullable = False ) // Set the length Public Gender getgender (){ Return Gender ;} Public Void Setgender (gender Gender ){ This . Gender = gender;} @ lob // Description of the big data field, which is a big text type on the string Public String getinfo (){ Return Info ;} Public Void Setinfo (string info ){ This . Info = Info;} @ lob // Big data field description, which is used to store binary data @ Basic (fetch = fetchtype. Lazy) // Delayed loading. The get method that accesses this attribute is loaded from the database to the memory, // At this time, you must ensure that EM is in the open state; otherwise, a delayed loading error occurs. Public Byte [] GetFile (){ Return File ;} Public Void Setfile (byte [] File ){ This . File = file ;} Public Void Setimagepathstring (string imagepathstring ){ This . Imagepathstring = imagepathstring;} @ transient // This attribute is not a persistent Field Public String getimagepathstring (){ Return Imagepathstring ;}}
Enumeration class:
PackageCom. Persia. JPA;PublicEnum gender {Man, woman}
4. Add a Test method:
Package JUnit. test; Import Java. util. date; Import Javax. Persistence. entitymanager; Import Javax. Persistence. entitymanagerfactory; Import Javax. Persistence. persistence; Import Org. JUnit. beforeclass; Import Org. JUnit. test; Import Com. Persia. JPA. person; Public Class Persontest {@ beforeclass Public Static Void Setupbeforeclass () Throws Exception {} // @ Test Public Void Save () {entitymanagerfactory FAC = persistence. createentitymanagerfactory (" Hellojpapu "); // At this time, the database table has been created. If there is no table, it is likely that the object annotation has an error. Entitymanager em = FAC. createentitymanager (); em. gettransaction (). Begin (); // Start the transaction Person = New Person (" Linda "); Person. setbirthday ( New Date (); em. persist (person); em. gettransaction (). Commit (); // Submit the transaction Em. Close (); FAC. Close ();} // @ Test Public Void Getperson (){ // You do not need to enable the transaction for reading. Entitymanagerfactory FAC = persistence. createentitymanagerfactory (" Hellojpapu "); Entitymanager em = FAC. createentitymanager (); person P = em. Find (person. Class , 1 ); // If no data exists, null is returned. System. Out. println (P. getname (); em. Close (); FAC. Close ();} // @ Test Public Void Getperson2 (){ // You do not need to enable the transaction for reading. Entitymanagerfactory FAC = persistence. createentitymanagerfactory (" Hellojpapu "); Entitymanager em = FAC. createentitymanager (); person P = em. getreference (person. Class , 2 ); // Delayed initialization. No record is obtained from the database immediately, but a proxy object is obtained. System. Out. println (P. getname ()); // When we access the properties of this proxy object, we get records from the database. At this time, we must ensure that EM is on; otherwise, delayed loading exceptions will occur. // If the object does not exist during access, the exception entitynotfoundexception is thrown. Em. Close (); FAC. Close ();} // @ Test Public Void Updateperson () {entitymanagerfactory FAC = persistence. createentitymanagerfactory (" Hellojpapu "); Entitymanager em = FAC. createentitymanager (); em. gettransaction (). Begin ();// Start the transaction Person P = em. Find (person. Class , 1); p. setname (" Helloworld "); // This can be updated at this time, mainly including: /*** 1. associated with the current transaction * 2. the status of the Entity Bean is managed. ** the state of the entity: * 1. create new * 2. managed management * 3. free (unmanageable) * 4. delete */ Em. gettransaction (). Commit (); // Submit the transaction Em. Close (); FAC. Close ();} // @ Test Public Void Updateperson2 () {entitymanagerfactory FAC = persistence. createentitymanagerfactory (" Hellojpapu "); Entitymanager em = FAC. createentitymanager (); em. gettransaction (). Begin (); // Start the transaction Person P = em. Find (person. Class , 1); em. Clear (); // Change all objects in the Entity Manager to a free State. In this case, use merge to update objects. P. setname (" Hello3 "); // This can be updated at this time, mainly including: Em. Merge (P ); // Synchronize updates to the entity bean in the Free State to the database. Em. gettransaction (). Commit (); // Submit the transaction Em. Close (); FAC. Close () ;}@ Test Public Void Delete () {entitymanagerfactory FAC = persistence. createentitymanagerfactory ("Hellojpapu "); Entitymanager em = FAC. createentitymanager (); em. gettransaction (). Begin (); // Start the transaction Person P = em. Find (person. Class , 1); em. Remove (P ); // Delete the managed Entity Bean Em. gettransaction (). Commit (); // Submit the transaction Em. Close (); FAC. Close ();}}