It Ninja Turtles hibernate combined primary key, primary key generation policy

Source: Internet
Author: User
Tags compact

If the database uses multiple fields instead of just one as the primary key, that is, the Federated primary key, this time you can use Hibernate to provide a federated primary key generation strategy.

Specific as follows:

You can use a component as an identifier for an entity class. Your component class must meet the following requirements:

    • It must implement  java.io.serializable   interfaces

    • It must be re-implemented  equals ()   and  hashcode ()   method, always consistent with the concept of the composite keyword in the database  

      Note: In Hibernate3, the second requirement is not Hibernate mandatory. But it is best to do so.

You cannot use a IdentifierGenerator generation combination keyword. An application must assign its own identifier.

The procedure is as follows:

1. Write a class that contains all the fields of the Federated primary key in the table, as the primary key class, as follows:

Package Com.seed.lee.model;

/**
* This class is a primary key class for the person class (combined primary key)
*
* @author Administrator
*
*/
public class Personuionpkid implements Java.io.Serializable {

Private String FirstName;

Private String Secondname;

Public String Getfirstname () {
return firstName;
}

public void Setfirstname (String firstName) {
This.firstname = FirstName;
}

Public String Getsecondname () {
return secondname;
}

public void Setsecondname (String secondname) {
This.secondname = Secondname;
}

@Override
public boolean equals (Object obj) {
if (obj instanceof personuionpkid) {
Personuionpkid pk= (personuionpkid) obj;
if (This.firstName.equals (pk.firstname) &&this.secondname.equals (pk.secondname)) {
return true;
}
}
return false;
}

@Override
public int hashcode () {
return Super.hashcode ();
}

}

It is important to note that the primary key class must implement the Java.io.Serializable interface, as described in the Hibernate API documentation, and it is recommended that you override the Equals () and Hashcode () methods in your own way to guarantee the uniqueness of the primary key.

2. Write a class that is equivalent to the table field, which does not necessarily contain all the fields of the primary key of the tables, as the following example:

Package Com.seed.lee.model;

public class Person {

Personuionpkid uionpkid = new Personuionpkid ();

private int age;

Private String sex;

Private String job;

Public Personuionpkid Getuionpkid () {
return uionpkid;
}

public void Setuionpkid (Personuionpkid uionpkid) {
This.uionpkid = Uionpkid;
}

public int getage () {
return age;
}

public void Setage (int.) {
This.age = age;
}

Public String Getsex () {
return sex;
}

public void Setsex (String sex) {
This.sex = sex;
}

Public String Getjob () {
return job;
}

public void Setjob (String job) {
This.job = job;
}

}

3. Configure the Hibernate configuration file as follows:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >

<class name= "person" table= "person" >

<composite-id name= "Uionpkid" class= "Com.seed.lee.model.PersonUionPKID" >
<key-property name= "Secondname"/>
<key-property name= "FirstName"/>
</composite-id>
<property name= "Age", column= "age"/>
<property name= "Sex" length= "2"/>
<property name= "Job" length= "/>"
</class>

4. Do not forget to reference the mapping file in the Hibernate configuration file.

<mapping resource= "Com/seed/lee/model/person.hbm.xml"/>

It Ninja Turtles hibernate combined primary key, primary key generation policy

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.