Now it is not recommended to use the Federated primary key, the key is because it needs their own manual maintenance, more trouble. But a project may be due to historical reasons, you have to face the joint primary key.
Hibernate Federated primary key issues are resolved as follows:
You can use a component as an identifier for an entity class. Your component class must meet the following requirements:
(1) It must implement the Java.io.Serializable interface
(2) It must re-implement the Equals () and Hashcode () methods, always consistent with the concept of the composite keyword in the database
Note: In Hibernate3, the second requirement is not required by Hibernate. But it is best to do so.
You cannot use a identifiergenerator to generate composite keywords. An application must assign its own identifier.
Extracts the corresponding property of the primary key to a class (called the primary Key Class), and the primary key class needs to implement the serializable interface, overriding the Equals method and the Hashcode method, for the reason:
Is that hibernate depends on the Federated primary key of the database to determine whether a two-row record is the same, and if it is the same object, if it is not, then it is considered a different object.
The procedure is as follows:
(1) Extract the corresponding attribute of the primary key to a class (called the primary Key Class), and the primary key class needs to implement the serializable interface, overriding the Equals method and the Hashcode method
Examples are as follows:
Package Com.model;import Java.io.serializable;//scoreinfo PRIMARY key classes//composite-id class must implement Serializable: Com.model.ScorePKpublic class SCOREPK implements Serializable{private static final long serialversionuid = 1L; Private integer studentid;private integer courseid;public int Getstudentid () {return studentid;} public void Setstudentid (int studentid) {this.studentid = StudentID;} public int Getcourseid () {return courseid;} public void Setcourseid (int courseid) {This.courseid = CourseID;} @Overridepublic boolean equals (Object o) {if (o instanceof scorepk) {scorepk SCOREPK = (SCOREPK) o;if (This.studentid = = Scorepk.studentid && This.courseid = = Scorepk.courseid) {return true;}} return false;} @Overridepublic int hashcode () {int result = 1;result = result + ((This.studentid = = null)? 0:this.studentid.hashcode ()) ; result = result + ((This.courseid = = null)? 0:this.courseid.hashcode ()); return result; }}
It is important to note here that the primary key class must implement the Java.io.Serializable interface, as described in the Hibernate API documentation, and override the Equals and Hashcode methods to guarantee the uniqueness of the primary key.
(2) Entity class (This does not include the attribute corresponding to the primary key)
Package Com.model;public class Scoreinfo {//Federated primary KEY private SCOREPK scorepk;private int score;public scorepk GETSCOREPK () {RE Turn SCOREPK;} public void Setscorepk (SCOREPK scorepk) {this.scorepk = SCOREPK;} public int Getscore () {return score;} public void SetScore (int score) {This.score = score;}}
(3) Configure Hibernate configuration file ScoreInfo.hbm.xml as follows:
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
If the table uses a federated primary key, you can map multiple properties of the class to the identifier property. The <composite-id> element accepts <key-property> attribute mappings and <key-many-to-one> attribute mappings as child elements.<composite-id name = "SCOREPK" class = "Com.model.ScorePK" ><key-property name= "StudentID" ></ Key-property><key-property name= "CourseID" ></key-property></composite-id>
This combined primary key is made up of StudentID and CourseID.
(4) Do not forget to reference the mapping file in the Hibernate configuration file.
<!--XML configuration-- <mapping resource= "Com/model/scoreinfo.hbm.xml"/>
(5) testPackage Com.test;import Org.hibernate.session;import Com.model.scoreinfo;import com.model.scorepk;import Com.util.hibernateutil;public class Test {public static void main (string[] args) {Test mgr = new Test (); Mgr.createandstore Event (); Hibernateutil.getsessionfactory (). Close (); } private void Createandstoreevent () { Session session = Hibernateutil.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); SCOREPK SCOREPK = new SCOREPK (); Scorepk.setstudentid (2); Scorepk.setcourseid (1); Scoreinfo score = new Scoreinfo (); SCORE.SETSCOREPK (SCOREPK); Score.setscore (98); Session.save (score); Session.gettransaction (). commit (); }}
Auxiliary class: HibernateutilPackage Com.util;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public class Hibernateutil {private static final sessionfactory sessionfactory; Static { try { //Create the sessionfactory from hibernate.cfg.xml sessionfactory = new Configuration (). Configure (). Buildsessionfactory (); } catch (Throwable ex) { //Make sure your log the exception, as it might be swallowed System.err.println ("Initi Al Sessionfactory creation failed. "+ ex); throw new Exceptionininitializererror (ex); } } public static Sessionfactory Getsessionfactory () { return sessionfactory; }}
Hibernate Learning Notes (5) Federated primary Key