Combined primary key generation policy in Hibernate

Source: Internet
Author: User
1. xml configuration a separate class is designed for the joint primary key as the primary key class, such as studentka, Serializable interface B, rewrite equals () and hashCode () why do I need to write equals () and hashCode () methods? The same hashCode will be stored in the same location of the hash table. After a specific hashcode is found

1. xml configuration A separate class is designed for the joint primary key 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 will be stored in the same location of the hash table. After a specific hashcode is found

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 pk = (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

 
 
  
   
   
   
    
    
   
   
   
  
 (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; @ BeforeClasspublic static void beforeClass () {sf = new AnnotationConfiguration (). configure (). buildSessionFactory () ;}@ Testpublic void testStudent () {StudentPK pk = new StudentPK (); pk. setId (1); pk. setName ("zhangsan"); Student s = new Student (); s. setPk (pk); // s. setName ("Zhang San"); s. setAge (20); s. setScore (90); Session session = sf. openSession (); session. beginTransaction (); session. save (s); session. getTransaction (). commit (); session. close () ;}@ AfterClasspublic 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;@Embeddablepublic 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; @ BeforeClasspublic static void beforeClass () {sf = new AnnotationConfiguration (). configure (). buildSessionFactory () ;}@ Testpublic 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. a); Session session = sf. openSession (); session. beginTransaction (); session. save (t); session. getTransaction (). commit (); session. close () ;}@ AfterClasspublic 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;} @ EmbeddedIdpublic TeacherPK getPk () {return pk;} public [this article is from Internet of Hong Kong ( http://www.68idc.cn ) 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 @ Id on Teacher Class Name

@IdClass(value=TeacherPK.class)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")@Idpublic String getName() {return name;}public void setName(String name) {this.name = name;}private TeacherPK pk;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;}/*@EmbeddedIdpublic TeacherPK getPk() {return pk;}public void setPk(TeacherPK pk) {this.pk = pk;}*/}

(2) test

(3) view results

Select * from teacher;

Desc teacher;

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.