方式一:
po类Teacher
package com.hibernate.demo.po; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name="teacher") public class Teacher { private TeacherPK pk; private String title; private String yourWifeName; private Date birthDate; private boolean good; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } @Temporal(TemporalType.TIME) public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } @Id public TeacherPK getPk() { return pk; } public void setPk(TeacherPK pk) { this.pk = pk; } }
po 复合主键类TeacherPK
package com.hibernate.demo.po; 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; } @Override public boolean equals(Object obj) { TeacherPK pk = (TeacherPK)obj; if(this.id==pk.getId() && this.name.equals(pk.getName())){ return true; }else{ return false; } } @Override public int hashCode() { return this.name.hashCode(); } }
测试方法:
package com.hibernate.demo.po; 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 TestTeacher { private static SessionFactory sf = null; @BeforeClass public static void beforeClass(){ sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @Test public void testSave(){ Session session = sf.openSession(); session.beginTransaction(); TeacherPK pk = new TeacherPK(); pk.setId(1); pk.setName("nameStr"); Teacher teacher = new Teacher(); teacher.setPk(pk); teacher.setBirthDate(new Date()); session.save(teacher); session.getTransaction().commit(); } @AfterClass public static void afterClass(){ sf.close(); } }
方式二(比第一种配置简单):
po类Teacher
package com.hibernate.demo.po; import java.util.Date; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name="teacher") public class Teacher { private TeacherPK pk; private String title; private String yourWifeName; private Date birthDate; private boolean good; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } @Temporal(TemporalType.TIME) public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } @EmbeddedId public TeacherPK getPk() { return pk; } public void setPk(TeacherPK pk) { this.pk = pk; } }
复合主键类TeacherPK
package com.hibernate.demo.po; 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; } @Override public boolean equals(Object obj) { TeacherPK pk = (TeacherPK)obj; if(this.id==pk.getId() && this.name.equals(pk.getName())){ return true; }else{ return false; } } @Override public int hashCode() { return this.name.hashCode(); } }
测试代码不变。。。
方式三:
package com.hibernate.demo.po; import java.util.Date; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name="teacher") @IdClass(value=TeacherPK.class) public class Teacher { // private TeacherPK pk; private int id; private String name; private String title; private String yourWifeName; private Date birthDate; private boolean good; @Id public int getId() { return id; } public void setId(int id) { this.id = 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; } public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } @Temporal(TemporalType.TIME) public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } // @EmbeddedId // public TeacherPK getPk() { // return pk; // } // public void setPk(TeacherPK pk) { // this.pk = pk; // } }
此处省略TeacherPK和测试代码。。