Hibernate Federated Primary Key

Source: Internet
Author: User

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

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.