Using composite primary keys in Hiberante

Source: Internet
Author: User

For the design and development of the new system, we should try to avoid introducing the primary key relationship related to business logic in the library table. The introduction of business logic primary key into the library table, the future changes in business logic, will likely have a joint impact on the underlying database structure. The introduction of composite primary keys largely means that business logic has penetrated into the logic of data storage. Therefore, it should be avoided as far as possible. However, in the actual situation, we must face the legacy system of the old table development, at this time, the existing composite primary key support is very necessary.

Hibernate, the composite primary key is defined by the Composite-id node.
We can determine the primary key in two ways:
1) Composite primary key based on attribute of entity class
2) Composite primary key based on primary key class

Here's a look at two ways to use it.

We built a user table, FirstName, LastName, age, to FirstName, LastName as the composite primary key. To build a table statement:
CREATE TABLE ' hbm_test '. ' User ' (
' FirstName ' VARCHAR (not NULL DEFAULT ' '),
' LastName ' VARCHAR (not NULL DEFAULT ' '),
' Age ' INTEGER UNSIGNED DEFAULT 0,
PRIMARY KEY (' FirstName ', ' LastName ')
)
ENGINE = InnoDB;

1) Composite primary key based on attribute of entity class

The mapping file is as follows:
<class name= "user" table= "user" >

<composite-id>
<key-property
Name= "Lastname"
column= "LastName"
Type= "string"
/>
<key-property
Name= "Firstname"
column= "FirstName"
Type= "string"
/>
</composite-id>

<property
Name= "Age"
Column= "Age"
Type= "Integer"
Not-null= "false"
Length= "10"
/>

</class>

A composite primary key is declared by the Composite-id node, which is made up of "FirstName" "LastName".

The Entity class user contains the composite primary key FirstName Lastname,hibernate requires the composite primary key class to implement the Equals hashcode as a token of recognition between different data.

public class User implements serializable{
Private String FirstName;
Private String LastName;
private int age;

public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String Getfirstname () {
return FirstName;
}
public void Setfirstname (String firstname) {
This.firstname = FirstName;
}
Public String Getlastname () {
return lastname;
}
public void Setlastname (String lastname) {
This.lastname = LastName;
}

public boolean equals (Object obj) {
if (! ( obj instanceof User) {
return false;
}else{
User user = (user) obj;
return new Equalsbuilder (). Appendsuper (Super.equals (obj))
. Append (This.firstname, user.firstname)
. Append (This.lastname, user.lastname)
. Isequals ();
}
}

public int hashcode () {
return new Hashcodebuilder (-528253723,-475504089)
. Appendsuper (Super.hashcode ())
. Append (This.firstname)
. Append (This.lastname)
. Tohashcode ();
}
}

Equalsbuilder Hashcodebuilder is a tool class in the Apache Common Lang package.

For the Session.load method, we can use the user class object itself as a query condition:

User user = new user ();
User.setfirstname ("Hello");
User.setlastname ("World");
user = (user) session.load (user.class, user);
System.out.println ("Age:" + user.getage ());

2) Composite primary key based on primary key class

We can separate the primary key logic with a single primary key class to describe the composite primary key.
Now extract the FirstName LastName from user into a separate primary key class USERPK:

public class USERPK implements serializable{
Private String FirstName;
Private String LastName;
Public String Getfirstname () {
return FirstName;
}
public void Setfirstname (String firstname) {
This.firstname = FirstName;
}
Public String Getlastname () {
return lastname;
}
public void Setlastname (String lastname) {
This.lastname = LastName;
}

}

Then modify the Composite-id node of the mapping file:
<composite-id name= "USERPK" class= "USERPK" >
<key-property
Name= "Lastname"
column= "LastName"
Type= "string"
/>
<key-property
Name= "Firstname"
column= "FirstName"
Type= "string"
/>
</composite-id>

<property
Name= "Age"
Column= "Age"
Type= "Integer"
Not-null= "false"
Length= "10"
/>

Just configure the name and class property, name specifies the primary key class property name in the entity class, and class specifies the primary key class type.

User.java modified as follows:
public class User implements serializable{
Private USERPK USERPK;
private int age;

public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public USERPK GETUSERPK () {
return USERPK;
}
public void Setuserpk (USERPK userpk) {
THIS.USERPK = USERPK;
}

}

After that, we can query the data via USERPK:
USERPK USERPK = new USERPK ();
Userpk.setfirstname ("Hello");
Userpk.setlastname ("World");
User user = (user) session.load (user.class, USERPK);
System.out.println ("Age:" + user.getage ());

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.