Hibernate joint primary key

Source: Internet
Author: User

In daily development, sometimes two or more fields are required to join the table as the primary key. For example, if two fields are used as the primary key, the joint primary key of the User table is firstname and lastname, the joint primary key of the commodity table is id and pid. In hibernate, composite-id nodes are also provided to support and define composite primary keys.
 
To make it more detailed, we create a new T_User table. The firstname and lastname are the joint primary keys of the table:
 
SQL code
Create table T_User (
FIRSTNAME varchar2 (10 ),
LASTNAME varchar2 (4 ),
Age number (2 ),
CONSTRAINT PK_TAB primary key (FIRSTNAME, LASTNAME) -- l sets the Union PRIMARY KEY
);
 
 
The hibernate ing file based on this table is: Xml Code
<Class name = "com. entity. TUser" table = "T_user">
<Composite-id>
<Key-property name = "lastname" column = "lastname" type = "string"/>
<Key-property name = "firstname" column = "firstname" type = "string"/>
</Composite-id>
 
<Property column = "age" name = "age" type = "integer"/>
</Class>
/Hibernate-mapping>
 
Hibernate requires that the compound primary key class implement the equal and hashcode methods as identification marks between different data, so the Tuser. java code should be as follows: public class Tuser implement Serializable {
Java code
Private Integer age;
Private String firstname;
Private String lastname;
 
// Getter and setter
 
Public boolean equals (Object obj ){
If (! (Obj instanceof Tuser )){
Return false;
}
Tuser tuser = (Tuser) obj;
Return new EqualsBuilder (). appendSuper (super. equals (obj )). append (this. lastname, tuser. lastname ). append (this. firstname, tuser. firstname ). isEquals ();
}
 
Public int hashCode (){
Return new HashCodeBuilder (-528253723,-475504089). appendSuper (super. hashCode (). append (this. lastname). append (this. firstname). toHashCode ();
}
 
}
 
The aforementioned producer builder and HashCodeBuilder are both tool classes in the apache commons lang package. Tuser contains composite component data, which is also the role of the "primary key class", that is, the entity identifier (id). Therefore, for Session. for the load () method, we can use the Tuser class object as the query condition (serialable id) for retrieval:
 
Java code
Tuser uesr = new Tuser ();
User. setFirstname ("Cat ");
User. setLastname ("Dog ");
// Identify the object with the user ID and load it using the load Method
User = (Tuser). session. load (Tuser. class, user );
Syso ("User age is" + uesr. getAge ());
 
 
The specific content of the introduced new node composite-id is:
Java code
<Composite-id
Name = "propertyName"
Class = "ClassName"
Mapped = "true | false"
Access = "field | property | ClassName"
Node = "element-name | ."
>
 
<Key-property name = "propertyName" type = "typename" column = "column_name"/>
<Key-extract-to-one name = "propertyName class =" ClassName "column =" column_name "/>
......
</Composite-id>
It contains the key-pair-to-one node, which is used to implement multiple-to-one association. Of course, this is not involved here. If you are interested, you can view the relevant information on your own.

Related Article

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.