1. Mapping rules for federated primary keys
1) Each primary key attribute in the class corresponds to each primary key column in the data table.
Hibernate requires an entity class with a federated primary key to implement the serializable interface, and overrides the hashcode with the Equals method, because hibernate determines whether a two-row record is the same based on the Federated primary key of the database. If it is the same, then it is considered to be the same object, and if it is not the same, then it is considered a different object. This is reflected in the field of procedure to determine whether an object can be placed into a set such as set based on the Hashcode and equals methods. The reason that the entity class of the Federated primary Key implements the Serializable interface is that the object of the entity needs to be built first when the get or Load method is used, and the query based (the Federated primary Key) is set in and then passed in as the second parameter of the Get or Load method.
2) 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, for the same reason as above.
Take the student class as an example to achieve the configuration of the two mapping federated primary keys:
Two properties in 2.Student as federated primary key properties
Student class:
public class Student implements Serializable {//must implement Serializable interface
Private String Cardid;//cardid and name are mapped to federated primary keys
private String name;
private int age;
Get, set, Hashcode, Equals method omitted
}
Note: You can use the Sourse-->gennerate hashcode and equals in myeclipse to quickly generate hashcode and the Equals method using MyEclipse
Student.hbm.xml configuration:
<class name= "Bean. Student "table=" Student ">
<composite-id><!--Federated primary key, student table with primary key (STUDENT_NAME,CARD_ID)--
<key-property name= "name" column= "Student_name" type= "string" ></key-property><!-- Name and Cardid are properties in the student class--
<key-property name= "CardID" column= "card_id" type= "string" ></key-property>
</composite-id>
<property name= "Age" column= "student_age" type= "int" ></property>
</class>
Save object:
Tx=session.begintransaction ();
Student s1=new Student ();
S1.setname ("Lisi");
S1.setage (22);
S1.setcardid ("711");
System.out.println (S1);
Session.save (S1);
Tx.commit ();
Note: If the primary key is (card_id,student_id) continuous execution of the above save statement two times, of course, will throw an exception, you should repeat the primary key:
Org.hibernate.exception.ConstraintViolationException:Could not execute JDBC batch update
3. Extract the two primary key attributes from the student class to a new class PrimaryKey, the primary key class
Primary KEY class PrimaryKey:
public class PrimaryKey implements serializable{
Private String CardID;
private String name;
Get, set, Hashcode, Equals method omitted
}
The Student class contains properties of the PrimaryKey type and corresponding set, get methods:
public class Student {
private int age;
Private PrimaryKey PrimaryKey;
Set, Get method omitted
}
Configuration in the Student.hbm.xml file:
<class name= "Bean. Student "table=" Student ">
<composite-id name= "PrimaryKey" class= "bean. PrimaryKey "><!--PrimaryKey for our custom primary key class--
<key-property name= "name" column= "Student_name" type= "string" ></key-property><!-- Name and Cardid are properties in the PrimaryKey class--
<key-property name= "CardID" column= "card_id" type= "string" ></key-property>
</composite-id>
<property name= "Age" column= "student_age" type= "int" ></property>
</class>
Save object:
Tx=session.begintransaction ();
Student s1=new Student ();
S1.setage (23);
PrimaryKey p=new PrimaryKey ();
P.setcardid ("102");
P.setname ("Zhangsan");
S1.setprimarykey (P);
Session.save (S1);
Tx.commit ();
Similarly, repeated executions of the above code cause the primary key to repeat the exception.
Inquire:
PrimaryKey p=new PrimaryKey ();
P.setcardid ("711");
P.setname ("Lisi");
Student s= (Student) session.get (student.class,p);//So PrimaryKey to implement serializable interface
System.out.println (S.getage ());
Reprint Please specify source: http://blog.csdn.net/jialinqiang/article/details/8704538
Application of the mapping of Federated primary keys