Hibernate learning -------- combining multiple fields for primary keys (for Annotation versions, xml is not commonly used)

Source: Internet
Author: User

Hibernate learning -------- combining multiple fields for primary keys (for Annotation versions, xml is not commonly used)

Here we record the annotation method of combining primary keys in hibernate.

Hibernate-annotations This document describes how to use annotation to combine primary keys.

The following statements define the primary key combination:

1. annotation of the component class as @ Embeddable, and Annotation of the Component Property as @ Id

2. annotation of component properties as @ EmbeddedId

3. annotation of the class as @ IdClass, and Annotation of all attributes belonging to the primary key in the object as @ Id

Here we will only introduce the two most commonly used methods (the second and the third)

(1) @ EmbeddedId

Create a new Teacher Class and add attributes to the class, including the primary key class TeacherPK

Package com. team. model; import javax. persistence. column; import javax. persistence. embeddedId; import javax. persistence. entity; @ Entitypublic class Teacher {private TeacherPK pk; // primary key class private String sex; @ EmbeddedId // primary key public TeacherPK getPk () {return pk ;} public void setPk (TeacherPK pk) {this. pk = pk;} @ Column (length = 20) public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}
Write the primary key class TeacherPK. java. Note that the Serializable interface is implemented and the equals and hashCode methods are rewritten.

Package com. team. model; import java. io. serializable; 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;} // two methods that must be overwritten @ Overridepublic boolean equals (Object obj) {if (obj instanceof TeacherPK) // first determine whether it is TeacherPK type {TeacherPK pk = (TeacherPK) obj; if (this. id = pk. getId () & this. name. equals (pk. getName () {return true ;}} return false ;}@ Overridepublic int hashCode () {return super. hashCode ();}}
Add a ing in hibernate. cfg. xml:


Right-click Teacher. java to create the Junit Test class: TeacherTest:

Package com. team. model; 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 TeacherTest {private static Configuration cfg = null; private static SessionFactory sf = null; @ BeforeClasspublic static void beforeClass () {Configuration cfg = new AnnotationConfiguration (); sf = cfg. configure (). buildSessionFactory () ;}@ AfterClasspublic static void afterClass () {sf. close () ;}@ Testpublic void test () {TeacherPK = new TeacherPK (); pk. setId (1); pk. setName ("Zhang San"); Teacher teacher = new Teacher (); teacher. setPk (pk); teacher. setSex ("male"); Session session = sf. openSession (); session. beginTransaction (); session. save (teacher); session. getTransaction (). commit (); session. close ();}}
Running result: (Table creation Statement)

Create table Teacher (id integer not null, name varchar (255) not null, sex varchar (20), primary key (id, name ))

View the MySQL database:

Vc/kernel + kernel/nT0LXE1ve8/kernel + csung vcd4kpha + kernel + csung vcd4kpha + CrPM0PLI58/Co7o8L3A + kernel =" brush: java; "> package com. team. model; import javax. persistence. column; import javax. persistence. embeddedId; import javax. persistence. entity; import javax. persistence. id; import javax. persistence. idClass; @ Entity @ IdClass (value = TeacherPK. class) public class Teacher {private int id; private String name; // private TeacherPK pk; // primary key class private String sex;/* @ EmbeddedId // primary key public TeacherPK getPk () {return pk;} public void setPk (TeacherPK pk) {this. pk = pk;} */@ Id // primary key 1 public int getId () {return id;} public void setId (int id) {this. id = id;} @ Id // primary key 2 public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Column (length = 20) public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}Test class:

Package com. team. model; 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 TeacherTest {private static Configuration cfg = null; private static SessionFactory sf = null; @ BeforeClasspublic static void beforeClass () {Configuration cfg = new AnnotationConfiguration (); sf = cfg. configure (). buildSessionFactory () ;}@ AfterClasspublic static void afterClass () {sf. close () ;}@ Testpublic void test () {/* TeacherPK = new TeacherPK (); pk. setId (1); pk. setName ("Zhang San"); */Teacher teacher = new Teacher (); teacher. setId (1); teacher. setName ("Li Si"); // teacher. setPk (pk); teacher. setSex ("male"); Session session = sf. openSession (); session. beginTransaction (); session. save (teacher); session. getTransaction (). commit (); session. close ();}}
The running result is the same as the previous one.




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.