Combined primary key generation policy in Hibernate, hibernate primary key

Source: Internet
Author: User

Combined primary key generation policy in Hibernate, hibernate primary key
I. Configure the joint primary key in xml

Design a class separately as the primary key class, such as StudentPK

A. Serializable Interface)

B. Rewrite equals () and hashCode ()

Why do I need to write equals () and hashCode () methods?

The same hashCode is stored in the same location of the hash table. After a specific hashcode is found, the corresponding data is searched based on the equals () method.

Lab 1:

(1) create a joint primary key class StudentPK

Package com. zgy. hibernate. model;

 

Import java. io. Serializable;

 

Public class StudentPK implements Serializable {

Private int id;

Private String name;

Public int getId (){

Return id;

}

Public void setId (int id ){

This. id = id;

}

Public String getName (){

Return name;

}

Public void setName (String name ){

This. name = name;

}

Public boolean equals (Object o ){

If (o instanceof StudentPK ){

StudentPK = (StudentPK) o;

If (this. id = pk. getId () & this. name = pk. getName ()){

Return true;

}

}

Return false;

}

Public int hashCode (){

Return this. name. hashCode ();

}

}

 

(2) configure the joint primary key using xml

<? 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>

 

<Hibernate-mapping package = "com. zgy. hibernate. model">

<Class name = "Student" table = "student">

<! -- <Id name = "id" column = "id">

<Generator class = "native"> </generator>

</Id>

<Property name = "name" column = "name"> </property>

-->

 

<! -- Configure the Union primary key -->

<Composite-id name = "pk" class = "com. zgy. hibernate. model. StudentPK">

<Key-property name = "id"> </key-property>

<Key-property name = "name"> </key-property>

</Composite-id>

<Property name = "age" column = "age"> </property>

<Property name = "score" column = "score"> </property>

</Class>

</Hibernate-mapping>

(3) Compile the test program

Package com. zgy. hibernate. model;

 

Import static org. junit. Assert .*;

 

Import java. util. Date;

 

Import org. hibernate. Session;

Import org. hibernate. SessionFactory;

Import org. hibernate. cfg. AnnotationConfiguration;

Import org. junit. AfterClass;

Import org. junit. BeforeClass;

Import org. junit. Test;

 

Public class HibernateIDTest {

 

Public static SessionFactory sf = null;

@ BeforeClass

Public static void beforeClass (){

Sf = new AnnotationConfiguration (). configure (). buildSessionFactory ();

}

@ Test

Public void testStudent (){

 

StudentPK = new StudentPK ();

Pk. setId (1 );

Pk. setName ("zhangsan ");

Student s = new Student ();

S. setPk (pk );

// S. setName ("James ");

S. setAge (20 );

S. setScore (90 );

Session session = sf. openSession ();

Session. beginTransaction ();

Session. save (s );

Session. getTransaction (). commit ();

Session. close ();

}

 

@ AfterClass

Public static void afterClass (){

Sf. close ();

}

 

}

 

2. Annotation Method 1: Use @ Embeddable

Configure @ Embeddable on the Union primary key class

In Teacher. java, write @ Id on getPk ()

Lab 2:

(1) Create TeacherPK. java

Package com. zgy. hibernate. model;

 

Import java. io. Serializable;

 

Import javax. persistence. Embeddable;

@ Embeddable

Public class TeacherPK implements Serializable {

Private int id;

Private String name;

Public int getId (){

Return id;

}

Public void setId (int id ){

This. id = id;

}

Public String getName (){

Return name;

}

Public void setName (String name ){

This. name = name;

}

Public boolean equals (Object o ){

If (o instanceof TeacherPK ){

TeacherPK pk = (TeacherPK) o;

If (this. id = pk. getId () & this. name = pk. getName ()){

Return true;

}

}

Return false;

}

Public int hashCode (){

Return this. name. hashCode ();

}

}

 

(2) test

Package com. zgy. hibernate. model;

 

Import static org. junit. Assert .*;

 

Import java. util. Date;

 

Import org. hibernate. Session;

Import org. hibernate. SessionFactory;

Import org. hibernate. cfg. AnnotationConfiguration;

Import org. hibernate. cfg. Configuration;

Import org. junit. AfterClass;

Import org. junit. BeforeClass;

Import org. junit. Test;

 

Public class TeacherTesting {

Public static SessionFactory sf = null;

@ BeforeClass

Public static void beforeClass (){

Sf = new AnnotationConfiguration (). configure (). buildSessionFactory ();

}

@ Test

Public void test (){

TeacherPK pk = new TeacherPK ();

Pk. setId (1 );

Pk. setName ("t1 ");

Teacher t = new Teacher ();

T. setPk (pk );

// T. setName ("t1 ");

T. setTitle ("advanced ");

T. setAddress ("Beijing ");

T. setBirth (new Date ());

T. setZhiCheng (ZhiCheng. );

Session session = sf. openSession ();

Session. beginTransaction ();

Session. save (t );

Session. getTransaction (). commit ();

Session. close ();

}

 

@ AfterClass

Public static void afterClass (){

Sf. close ();

}

}

 

(3) view results

 

Select * from teacher;

Desc teacher;

 

 

 

Method 2: Add @ EmbeddedId TO THE METHOD

Add @ EmbeddedId directly to the getPK () method in Teacher. java.

Lab 3:

(1) Modify Teacher. java

Package com. zgy. hibernate. model;

 

Import java. util. Date;

 

Import javax. annotation. Generated;

Import javax. persistence. Column;

Import javax. persistence. EmbeddedId;

Import javax. persistence. Entity;

Import javax. persistence. EnumType;

Import javax. persistence. Enumerated;

Import javax. persistence. GeneratedValue;

Import javax. persistence. GenerationType;

Import javax. persistence. Id;

Import javax. persistence. Table;

Import javax. persistence. Temporal;

Import javax. persistence. TemporalType;

Import javax. persistence. Transient;

 

@ Entity

@ Javax. persistence. TableGenerator (

Name = "Teacher_GEN ",

Table = "GENERATOR_TABLE ",

PkColumnName = "pkkey ",

ValueColumnName = "pkvalue ",

PkColumnValue = "Teacher ",

AllocationSize = 1

)

Public class Teacher {

// Private int id;

// Private String name;

Private String title;

Private String address;

Private String wifeName;

Private Date birth;

Private ZhiCheng zhiCheng;

@ Id

@ GeneratedValue (strategy = GenerationType. TABLE, generator = "Teacher_GEN ")

// Public int getId (){

// Return id;

//}

// Public void setId (int id ){

// This. id = id;

//}

// @ Column (name = "_ name ")

// Public String getName (){

// Return name;

//}

// Public void setName (String name ){

// This. name = name;

//}

Private TeacherPK;

Public String getTitle (){

Return title;

}

Public void setTitle (String title ){

This. title = title;

}

Public String getAddress (){

Return address;

}

Public void setAddress (String address ){

This. address = address;

}

 

Public String getWifeName (){

Return wifeName;

}

Public void setWifeName (String wifeName ){

This. wifeName = wifeName;

}

@ Temporal (TemporalType. DATE)

Public Date getBirth (){

Return birth;

}

Public void setBirth (Date birth ){

This. birth = birth;

}

@ Enumerated (EnumType. STRING)

Public ZhiCheng getZhiCheng (){

Return zhiCheng;

}

Public void setZhiCheng (ZhiCheng zhiCheng ){

This. zhiCheng = zhiCheng;

}

@ EmbeddedId

Public TeacherPK getPk (){

Return pk;

}

Public void setPk (TeacherPK pk ){

This. pk = pk;

}

 

}

 

(2) test

(3) view results

 

Select * from teacher;

Desc teacher;

 

 

 

Method 3:

(1) Modify in Teacher. java, add @ Id on getId (), getName (), and add @ IdClass (value = TeacherPK. class) on the Teacher class Name)

Package com. zgy. hibernate. model;

 

Import java. util. Date;

 

Import javax. annotation. Generated;

Import javax. persistence. Column;

Import javax. persistence. EmbeddedId;

Import javax. persistence. Entity;

Import javax. persistence. EnumType;

Import javax. persistence. Enumerated;

Import javax. persistence. GeneratedValue;

Import javax. persistence. GenerationType;

Import javax. persistence. Id;

Import javax. persistence. IdClass;

Import javax. persistence. Table;

Import javax. persistence. Temporal;

Import javax. persistence. TemporalType;

Import javax. persistence. Transient;

 

@ Entity

@ Javax. persistence. TableGenerator (

Name = "Teacher_GEN ",

Table = "GENERATOR_TABLE ",

PkColumnName = "pkkey ",

ValueColumnName = "pkvalue ",

PkColumnValue = "Teacher ",

AllocationSize = 1

)

@ IdClass (value = TeacherPK. class)

Public class Teacher {

Private int id;

Private String name;

Private String title;

Private String address;

Private String wifeName;

Private Date birth;

Private ZhiCheng zhiCheng;

@ Id

// @ GeneratedValue (strategy = GenerationType. TABLE, generator = "Teacher_GEN ")

Public int getId (){

Return id;

}

Public void setId (int id ){

This. id = id;

}

@ Column (name = "_ name ")

@ Id

Public String getName (){

Return name;

}

Public void setName (String name ){

This. name = name;

}

Private TeacherPK;

Public String getTitle (){

Return title;

}

Public void setTitle (String title ){

This. title = title;

}

Public String getAddress (){

Return address;

}

Public void setAddress (String address ){

This. address = address;

}

 

Public String getWifeName (){

Return wifeName;

}

Public void setWifeName (String wifeName ){

This. wifeName = wifeName;

}

@ Temporal (TemporalType. DATE)

Public Date getBirth (){

Return birth;

}

Public void setBirth (Date birth ){

This. birth = birth;

}

@ Enumerated (EnumType. STRING)

Public ZhiCheng getZhiCheng (){

Return zhiCheng;

}

Public void setZhiCheng (ZhiCheng zhiCheng ){

This. zhiCheng = zhiCheng;

}

/*

@ EmbeddedId

Public TeacherPK getPk (){

Return pk;

}

Public void setPk (TeacherPK pk ){

This. pk = pk;

}

*/

}

 

(2) test

(3) view results

 

Select * from teacher;

Desc teacher;

 


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.