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