Hibernate一对一主键关联(基于annotation注解方式)
来源:互联网 发布:云南师范大学知乎 编辑:程序博客网 时间:2024/05/16 01:08
hibernate中一对一的关联有两种方式:一种是采用外键关联,另外一种是采用主键关联。
最近项目中用到一对一主键双向关联,并且是基于注解的方式。期间遇到一些问题,现在贴出来探讨探讨。
一个丈夫(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表中记录的主键一致。
- Hibernate一对一主键关联(基于annotation注解方式)
- Hibernate一对一主键关联(基于annotation注解方式)
- Hibernate一对一主键关联(基于annotation注解方式)
- Hibernate Annotation 一对一主键关联
- Hibernate一对一关联(共享主键) 注解 || xml
- 【hibernate框架】一对一单向主键关联(Annotation实现)
- Hibernate一对一主键关联
- hibernate一对一主键关联
- hibernate annotation (注解映射) 测试二 一对一 关联
- MyBatis框架基于Annotation注解的一对一关联映射
- hibernate双向一对一主键关联映射XML与注解版
- Hibernate 一对一双向关联 Annotation
- Hibernate之基于主键映射的一对一关联关系
- Hibernate 一对一主键关联实例
- Hibernate 一对一主键单向关联
- Hibernate 一对一主键双向关联
- Hibernate一对一关联(共享主键)
- Hibernate 一对一主键单向关联
- Linux内核wait_queue深入分析
- offsetParent解释
- Android UI事件处理
- 关机回调
- 临时表操作的一些见解(解决了我在存储过程中使用临时表的困惑)
- Hibernate一对一主键关联(基于annotation注解方式)
- 回味下scrum
- memset
- Android 無線啟動流程
- 用Struts2实现文件上传时显示进度条功能
- 详细解析Linux目录结构
- ireport使用javabean作为数据源的实现
- DNW应用程序错误,DNW错误问题解决
- Android 的输入法框架