有3种方式:
1.@Embeddedable
2.@EmbeddedId
3. @IdClass
2,3 最常用
一, @Embeddedable
1.新建TeacherPK.java, 加入@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. 重写Teacher.java, 加入pk对象, 并设置@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;
@Id
public 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. 重写junit测试方法:
@Test
public void testTeacherSave() {
TeacherPK pk=new TeacherPK();
pk.setId(1);
pk.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();
}
二: @EmbeddedId
1. 去掉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的地方改成@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();
@EmbeddedId
public 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方法:
@Test
public void testTeacherSave() {
TeacherPK pk=new TeacherPK();
pk.setId(2);
pk.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();
}
三, @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. Teacher.java里类前加入IdClass:
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
@GeneratedValue
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
3. junit的测试方法;
@Test
public 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();
}