Hibernate Federated Primary Key mapping

Source: Internet
Author: User

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:

    1. Public class Student implements Serializable {//must implement Serializable interface
    2. private String CardID; //cardid and name mappings to federated primary keys
    3. private String name;
    4. private int age;
    5. //get, set, Hashcode, Equals method omitted
    6. }

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:

  1. <class name="Bean. Student " table="Student ">
  2. <Composite-id><!--Federated primary key, student table with primary key (student_name,card_id) --
  3.            <key-property name= "name"  < span class= "attribute" >column= "student_name"  type=< Span class= "Attribute-value" > "string" ></ Key-property><!--name and Cardid attributes in student class-->  
  4. <key-property name="CardID" column="card_id" type="string"></ key-property>
  5. </composite-id>
  6. < name="age" column="student_age" type="int"></ Property>
  7. </class>

Save object:

    1. Tx=session.begintransaction ();
    2. Student s1=New Student ();
    3. S1.setname ("Lisi");
    4. S1.setage (22);
    5. S1.setcardid ("711");
    6. System.out.println (S1);
    7. Session.save (S1);
    8. 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:

    1. Public class PrimaryKey implements serializable{
    2. private String CardID;
    3. private String name;
    4. //get, set, Hashcode, Equals method omitted
    5. }

The Student class contains properties of the PrimaryKey type and corresponding set, get methods:

    1. Public class Student {
    2. private int age;
    3. private PrimaryKey PrimaryKey;
    4. //set, get method omitted
    5. }

Configuration in the Student.hbm.xml file:

  1. <class name="Bean. Student " table="Student ">
  2.     <composite-id  name= "PrimaryKey"  class=< Span class= "Attribute-value" ">" bean. PrimaryKey "><!--PrimaryKey for our custom primary key class-->  
  3.        < Key-property name= "name"   column= "student_name"  type= "string" ></ Key-property><!--name and Cardid attributes in PrimaryKey class-->  
  4. <key-property name="CardID" column="card_id" type="string"></ key-property>
  5. </composite-id>
  6. < name="age" column="student_age" type="int"></ Property>
  7. </class>

Save object:

    1. Tx=session.begintransaction ();
    2. Student s1=New Student ();
    3. S1.setage (23);
    4. PrimaryKey p=New PrimaryKey ();
    5. P.setcardid ("102");
    6. P.setname ("Zhangsan");
    7. S1.setprimarykey (P);
    8. Session.save (S1);
    9. Tx.commit ();

Similarly, repeated executions of the above code cause the primary key to repeat the exception.

Inquire:

    1. PrimaryKey p=New PrimaryKey ();
    2. P.setcardid ("711");
    3. P.setname ("Lisi");
    4. Student s= (Student) session.get (Student. CLASS,P); //So PrimaryKey to implement serializable interface
    5. System.out.println (S.getage ());

4. For the two ways to map a federated primary key, the structure of the tables reflected in the database is the same, and the contents of the student table are:

Reprint Please specify source: http://blog.csdn.net/jialinqiang/article/details/8704538

Hibernate Federated Primary Key mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.