1. Configure the primary key class
Import Java. Io. serializable; Import Javax. Persistence. column; Import Javax. Persistence. embeddable; /*** 1. Provide the parameter-free constructor * 2. Implement the serial number interface * 3. Rewrite the hashcode and equals Methods * @ author administrator **/ @ Embeddable // Used in the object to tell JPA to only use the attributes in the class as persistence Fields Public Class Airlinepk Implements Serializable { Private String startcitystring; // Beijing pek Private String endcitystring; Public Airlinepk (){} Public Airlinepk (string endcitystring, string startcitystring ){ This . Endcitystring = endcitystring; This . Startcitystring = startcitystring;} @ column (length = 3) Public String getstartcitystring (){ Return Startcitystring ;} Public Void Setstartcitystring (string startcitystring ){ This . Startcitystring = startcitystring;} @ column (length = 3)Public String getendcitystring (){ Return Endcitystring ;} Public Void Setendcitystring (string endcitystring ){ This . Endcitystring = endcitystring;} @ override Public Int Hashcode (){ Final Int Prime = 31; Int Result = 1; Result = prime * result + (endcitystring = Null )? 0: endcitystring. hashcode (); Result = prime * result + (startcitystring = Null )? 0: startcitystring. hashcode ()); Return Result;} @ override Public Boolean Equals (Object OBJ ){ If ( This = OBJ) Return True ; If (OBJ = Null ) Return False ;If (Getclass ()! = Obj. getclass ()) Return False ; Airlinepk Other = (airlinepk) OBJ; If (Endcitystring = Null ){ If (Other. endcitystring! = Null ) Return False ;} Else If (! Endcitystring. Equals (other. endcitystring )) Return False ;If (Startcitystring = Null ){ If (Other. startcitystring! = Null ) Return False ;} Else If (! Startcitystring. Equals (other. startcitystring )) Return False ; Return True ;}}
2. Configure the object Bean
Import Javax. Persistence. column;Import Javax. Persistence. embeddedid; Import Javax. Persistence. entity; @ entity Public Class Airline { Private Airlinepk ID; Private String name; Public Airline (){ // Todo auto-generated constructor stub } Public Airline (string endcitystring, string startcitystring, string name ){ This . ID = New Airlinepk (startcitystring, endcitystring ); This . Name = Name;} @ embeddedid // ID of the object Public Airlinepk GETID (){ Return ID ;} Public Void Setid (airlinepk ID ){ This . ID = ID;} @ column (length = 20) Public String getname (){ Return Name ;} Public Void Setname (string name ){ This . Name = Name ;}}
3. JUnit Testing
Package JUnit. test; Import Javax. Persistence. entitymanager; Import Javax. Persistence. entitymanagerfactory; Import Javax. Persistence. persistence; Import Org. JUnit. beforeclass; Import Org. JUnit. test; Import Com. Persia. Bean. Airline; Import Com. Persia. Bean. airlinepk; Import Com.sun.org. Apache. bcel. Internal. Generic. New; Public Class Compositepktest {@ beforeclass Public Static Void Setupbeforeclass () Throws Exception {} // @ Test Public Void Save () {entitymanagerfactory factory = persistence. createentitymanagerfactory (" Jpacompositepkpu "); Entitymanager = factory. createentitymanager (); entitymanager. gettransaction (). Begin (); entitymanager. persist ( New Airline (" Pek ","Sha "," Beijing to Shanghai "); Entitymanager. gettransaction (). Commit (); entitymanager. Close (); factory. Close ();} // @ Test Public Void Getairline () {entitymanagerfactory factory = persistence. createentitymanagerfactory (" Jpacompositepkpu "); Entitymanager = factory. createentitymanager (); // Entitymanager. gettransaction (). Begin (); Airline Aline = entitymanager. Find (airline. Class , New Airlinepk (" Sha ","Pek "); System. Out. println (" The queried route: "+ Aline. getname ()); // Entitymanager. gettransaction (). Commit (); Entitymanager. Close (); factory. Close ();} // @ Test Public Void Updateairline () {entitymanagerfactory factory = persistence. createentitymanagerfactory (" Jpacompositepkpu "); Entitymanager = factory. createentitymanager (); entitymanager. gettransaction (). Begin (); airline Aline = entitymanager. Find (airline. Class , New Airlinepk ("Sha "," Pek "); Aline. setname (" BJ-SH "); Aline. setid ( New Airlinepk (" Sh "," BJ ")); // The primary key cannot be modified. Entitymanager. Merge (Aline); entitymanager. gettransaction (). Commit (); entitymanager. Close (); factory. Close () ;}@ Test Public Void Deleteairline () {entitymanagerfactory factory = persistence. createentitymanagerfactory (" Jpacompositepkpu "); Entitymanager = factory. createentitymanager (); entitymanager. gettransaction (). Begin (); airline Aline = entitymanager. Find (airline. Class , New Airlinepk (" Sha "," Pek "); Entitymanager. Remove (Aline); entitymanager. gettransaction (). Commit (); entitymanager. Close (); factory. Close ();}}