hibernate--combined PRIMARY Key--annotation

Source: Internet
Author: User

There are 3 ways of doing this:

[Email protected]

[Email protected]

3. @IdClass

2,3 most commonly used

One, @Embeddedable

1. Create a new Teacherpk.java and join @embeddable:

Package Com.bjsxt.hibernate;import javax.persistence.Embeddable; @Embeddable public class TEACHERPK implements java.io.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;    }  }

2. Rewrite the Teacher.java, add the PK object, and set the @id:

Package Com.bjsxt.hibernate;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import javax.persistence.id;import javax.persistence.Table, @Entity @table (name= " _teacher ") public class Teacher {private TEACHERPK pk; @Idpublic teacherpk getpk () {return pk;} public void Setpk (TEACHERPK pk) {this.pk = PK;} private string Title;public string GetTitle () {return title;} public void Settitle (String title) {this.title = title;}}

3. Rewrite the JUnit test method:

@Testpublic void Testteachersave () {TEACHERPK pk=new teacherpk ();p K.setid (1);p k.setname ("Lisi"); Teacher T =new Teacher (); T.SETPK (PK); T.settitle ("higfdfsdfsadh"); Session session =  sf.opensession (); Session.begintransaction (); Session.save (t); Session.gettransaction (). Commit (); Session.close ();}

Two: @EmbeddedId

1. Remove the Teacherpk.java @embeddable:

Package Com.bjsxt.hibernate;import javax.persistence.Embeddable; public class TEACHERPK implements java.io.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;    }  }

  

2. Teacher.java @id to @embeddedid

Package Com.bjsxt.hibernate;import Javax.persistence.embeddedid;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.id;import Javax.persistence.idclass;import Javax.persistence.Table, @Entity @table (name= "_teacher")//@IdClass (Value=teacherpk.class) public class Teacher { Private TEACHERPK PK = new TEACHERPK (); @EmbeddedIdpublic teacherpk getpk () {return pk;} public void Setpk (TEACHERPK pk) {this.pk = PK;} private string Title;public string GetTitle () {return title;} public void Settitle (String title) {this.title = title;}}

  

3. Test method:

@Testpublic void Testteachersave () {TEACHERPK pk=new teacherpk ();p K.setid (2);p k.setname ("Lisi"); Teacher T =new Teacher (); T.SETPK (PK); T.settitle ("higfdfsdfsadh"); Session session =  sf.opensession (); Session.begintransaction (); Session.save (t); Session.gettransaction (). Commit (); Session.close ();}

  

Three, @IdClass

1. Teacherpk.java:

Package Com.bjsxt.hibernate;import javax.persistence.Embeddable; public class TEACHERPK implements java.io.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;    }  }

2. Join the Idclass before the Teacher.java class:

Package Com.bjsxt.hibernate;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.id;import javax.persistence.idclass;import javax.persistence.Table, @Entity @table (name= "_ Teacher ") @IdClass (value=teacherpk.class) public class Teacher {private int id;private string Name;private string title;@ id@generatedvaluepublic 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 String GetTitle () {return title;} public void Settitle (String title) {this.title = title;}}

3. JUnit's test methods;

@Testpublic void Testteachersave () {Teacher T =new Teacher (); T.setid (1); T.setname ("Zhagnsan"); T.settitle (" Higfdfsdfsadh "); Session session =  sf.opensession (); Session.begintransaction (); Session.save (t); Session.gettransaction (). Commit (); Session.close ();}

  

hibernate--combined PRIMARY Key--annotation

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.