First, hibernate union the rules of the primary key class
1. Implement the Serializable interface
2. Overriding the hashcode and equals method
Two, Hibernate union primary key entity class Rule reason (corresponds to the above rule order)
1. 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 not, 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 according to the Hashcode and equals methods;
2. When using the Get or Load method, you need to build the object of the entity first, and set the query by (the Federated primary Key), and the second parameter of the GET or load method needs to be serialized.
Public Object get (Class clazz,serializable ID)
Third, hibernate combined with the use of the primary key
Primarykey.java
Student.java
public class Student { //attribute private PrimaryKey PrimaryKey; Union primary key Class private int age; Set, Get method omitted }
Student.hbm.xml
<class name= "Bean. Student "table=" Student "> <!--primaryKey for the custom primary key class-- <composite-id name=" PrimaryKey "class=" Bean. PrimaryKey "> <!--name and Cardid properties in the PrimaryKey class-- <key-property name=" name "column=" Student_ Name "type=" string "></key-property> <key-property name=" CardID "column=" card_id "type=" string " ></key-property> </composite-id> <property name= "age" column= "student_age" type= "int" ></property> </class>
Hibernate in use
Gets the public Student get (Serializable id) throws Exception {Object obj = this.gethibernatetemplate (). Get (Student.class,id if (obj = = null) {return null;} else {return (termdiscountconfig) obj;}}
Hibernate Federated Primary Key