最近项目中用到一对一主键双向关联,并且是基于注解的方式。期间遇到一些问题,现在贴出来探讨探讨。
一个丈夫(husband)对应一个妻子(wife),主要目标是在存储丈夫或者妻子时,关联的对象也被存储。具体代码如下:
husband类:
package cn.edu.dlnu.resources.model.entity;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue //主键生成器
public int getId() {
return id;
}
public String getName() {
return name;
}
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn//这个注解只能写在主(生成ID)的一端
public Wife getWife() {
return wife;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
wife类:
package cn.edu.dlnu.resources.model.entity;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
public class Wife {
private int id;
private String name;
private Husband husband;
@Id
@GenericGenerator(name ="pkGenerator",strategy="foreign" ,parameters={@Parameter(name="property",value="husband")})
@GeneratedValue(generator="pkGenerator")
//wife的ID是根据husband的ID来赋值的,这里需要设置ID生成器的策略为foreign,参数中指定wife的ID是使用husband对象中的ID
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 void setHusband(Husband husband) {
this.husband = husband;
}
@OneToOne(cascade=CascadeType.ALL, mappedBy="wife")
public Husband getHusband() {
return husband;
}
}
OneToOnePKTest类:
package cn.edu.dlnu.resources.model.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.edu.dlnu.resources.model.entity.Husband;
import cn.edu.dlnu.resources.model.entity.Wife;
public class OneToOnePKTest {
private static SessionFactory sessionFactory;
@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sessionFactory.close();
}
@Test
public void testSave(){
Session s = sessionFactory.getCurrentSession();
s.beginTransaction();
Husband h = new Husband();
Wife w = new Wife();
w.setName("w");
h.setName("h");
h.setWife(w);
w.setHusband(h);
s.save(w);
s.getTransaction().commit();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
public static void main(String[] args) {
beforeClass();
new OneToOnePKTest().testSave();
afterClass();
}
}
hibernate生成的数据库表:create table Husband (
id integer not null auto_increment,
name varchar(255),
primary key (id)
);
create table Wife (
id integer not null,
name varchar(255),
primary key (id)
)
运行testSave(),方法后数据库中husband 与 wife表中记录的主键一致。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于