Hibernate joint primary Key detailed _hibernate

Source: Internet
Author: User


Hibernate Federated primary Key mappings

1. Implement one: Combine the properties of a composite primary key with other common attributes of the entity

2. Implementation mode Two: Extract the primary key attribute into a primary key class, and the entity class simply contains a reference to the primary key class


Mapping rules for federated primary keys

1 each primary key attribute in the class corresponds to each primary key column in the datasheet.

Hibernate requires that entity classes with federated primary keys implement serializable interfaces and override the Hashcode and Equals methods.

1. Entity classes that use composite primary keys must implement the Serializable interface. The reason that the serializable interface must be implemented is simple, and we look up the data based on the primary key. Open the Hibernate help document we can find the form of a get and load method as follows:

Object load (Class theclass,serializable ID)

Object Get (Class theclass,serializable ID)

When we look up an object for a composite primary key class, we need to pass the primary key value to the get () or the load () method's ID parameter, and the ID parameter can only receive an object that implements the serializable interface. The primary key for a composite primary key class is not a property that can be represented. So you can only create an instance of a composite primary key class (for example: New people ()), and then assign the primary key value to the primary key property using the set method of the primary key property, and then pass the entire object to the ID parameter of the get () or load () method To implement the delivery of the primary key value, the entity class of the composite primary key must implement the serializable interface.

Application Scenario:

Why the primary key class should be serialized. If multiple objects of this class are put into memory at the same time, in a clustered system where one server is machine, the memory object needs to be written to another server. Also, if the server has full memory, virtual memory is required, which requires serialization before it can be written to the hard disk.

2. Entity classes that use composite primary keys must override the Equals and Hashcode methods. It is also well understood that the equals and hashcode methods must be overridden. These two methods are used to determine whether two objects (two records) are equal. Why should you judge whether two objects are equal? Because the primary key values in any two records in the database cannot be the same, so we can prevent a PRIMARY KEY constraint violation error by ensuring that the primary key value of two objects is different in the program (in the program, you need to override the hashcode () and equal () methods to ensure that the object's uniqueness is unique.) Maybe here you'll wonder why entity classes that do not use composite primary keys do not override both methods and do not have a primary key violation. This is because the primary key value is hibernate to be maintained using a single primary key, and it ensures that the primary key is not duplicated, and that the composite primary key is the way in which the primary key value is maintained by the programmer. Therefore, you must override the Equals and Hashcode methods to determine whether the primary keys of two objects are the same.

3. The overridden Equals and Hashcode methods are only related to primary key properties, and normal properties do not affect the two methods for judgment. The reason for this is simple, the primary key can determine a record, the other attributes can not determine a record.

2 extracts a primary key corresponding attribute of a class (called the primary Key Class), and the primary key class needs to implement the Serializable interface, rewrite the Equals method and the Hashcode method, the same reason as above.



One way: the properties corresponding to the composite primary key are put together with the other normal properties of the entity

For example, the entity class people the "id" and "name" attributes corresponding to the composite primary key:

Java code

1. Public class people implements Serializable {

2. Private static final long serialversionuid = -4888836126783955019l;

3. Private String ID;

4. Private String name;

5. private int age;

6.

7. Public people () {

8.}

9.

Public String getId () {

One. return ID;

12.}

13.

public void SetId (String id) {

This.id = ID;

16.}

17.

Public String GetName () {

return name;

20.}

21st.

public void SetName (String name) {

THIS.name = name;

24.}

25.

Num. public int getage () {

return-age;

28.}

29.

public void Setage (int age) {

This.age = age;

32.}

33.

@Override

public int hashcode () {

final int prime = 31;

(a) int result = 1;

result = Prime * result + ((id = = null) 0:id.hashcode ());

result = Prime * result + ((name = = null) 0:name.hashcode ());

return result;

41.}

42.

@Override

public boolean equals (Object obj) {

if (this = = obj)

return true;

The. if (obj = null)

return false;

if (GetClass ()!= obj.getclass ())

return false;

People other = (people) obj;

if (id = = NULL) {

if (other.id!= null)

return false;

N. else if (!id.equals (other.id))

return false;

if (name = = null) {

Other.name. if (!= null)

return false;

/Else if (!name.equals (other.name))

return false;

return true;

63.}

64.}


=======people.hbm.xml:

XML code

1. <?xml version= "1.0" encoding= "Utf-8"?>

2. <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

3.

4. <class name= "Com.suxiaolei.hibernate.pojos.People" table= "People" >

5. <!--Composite PRIMARY key uses Composite-id label-->

6. <composite-id>

7. <!--key-property label indicates which properties correspond to the composite primary key-->

8. <key-property name= "id" column= "id" type= "string" >

9. </key-property>

10.<key-property name= "name" column= "name" type= "string" >

11.</key-property>

12.</composite-id>

13.<property name= "Age" column= "Age" type= "integer" >

14.</property>

15.</class>

16.



Method Two: Extract the primary key attribute into a primary key class, the entity class simply contains a reference to the primary key class

Primary KEY class:

======peopleprimarykey.java

Java code

1. Import java.io.Serializable;

2.

3./* must implement SERIALIZABLE interface * *

4. Public class Peopleprimarykey implements Serializable {

5. Private static final long serialversionuid = -1190986010439330142l; /* Compound PRIMARY KEY value * *

6. Private String ID;

7. Private String name;

8.

9. Public Peopleprimarykey () {

10.///* Compound PRIMARY key value's get and set method * *

11.

A. Public String getId () {

return ID;

14.}

15.

public void SetId (String id) {

This.id = ID;

18.}

19.

Public String GetName () {

return name;

22.}

23.

public void SetName (String name) {

THIS.name = name;

26.}

27.

@Override

public int hashcode () {

final int prime = 31;

result int = 1;

result = Prime * result + ((id = = null) 0:id.hashcode ());

result = Prime * result + ((name = = null) 0:name.hashcode ());

return result;

35.}

36.

Panax @Override

public boolean equals (Object obj) {

if (this = = obj)

return true;

/if (obj = null)

return false;

if (GetClass ()!= obj.getclass ())

return false;

Peopleprimarykey other = (peopleprimarykey) obj;

if (id = = NULL) {

if (other.id!= null)

return false;

(!id.equals) Else if (other.id)

return false;

The. if (name = = null) {

if (other.name!= null)

return false;

{Else if (!name.equals (other.name))

return false;

return true;

57.}

58.}


===people.java

Java code

1. Public class People {/* holds a reference to the primary key class, which is used as the OID of this class.

2. Private Peopleprimarykey Peopleprimarykey;

3. private int age;

4.

5. Public people () {

6.}

7.

8. Public Peopleprimarykey Getpeopleprimarykey () {

9. return peopleprimarykey;

10.}

11.

public void Setpeopleprimarykey (Peopleprimarykey peopleprimarykey) {

This.peopleprimarykey = Peopleprimarykey;

14.}

15.

public int getage () {

return-age;

18.}

19.

public void Setage (int age) {

This.age = age;

22.}

23.}


===people.hbm.xml file slightly change:

Java code

1. <?xml version= "1.0" encoding= "Utf-8"?>

2. <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

3.

4. <class name= "Com.suxiaolei.hibernate.pojos.People" table= "People" >

5. <!--Composite PRIMARY key uses Composite-id label-->

6. <!--name-Specifies which property class the compound primary key corresponds to-specifies the type of the composite primary key property-->

7. <composite-id name= "Peopleprimarykey" class= "Com.suxiaolei.hibernate.pojos.PeoplePrimaryKey" >

8. <!--key-property Specifies which properties the composite primary key consists of-->

9. <key-property name= "id" column= "id" type= "string" >

10.</key-property>

11.<key-property name= "name" column= "name" type= "string" >

12.</key-property>

13.</composite-id>

14.<property name= "Age" column= "Age" type= "integer" >

15.</property>

16.</class>

17.


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.