Hibernate一对一主键关联(基于annotation注解方式)
2014-06-20 16:48
477 查看
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表中记录的主键一致。
转自:点击打开链接
最近项目中用到一对一主键双向关联,并且是基于注解的方式。期间遇到一些问题,现在贴出来探讨探讨。
一个丈夫(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关系映射(二)一对一双向外键关联@OneToOne Annotation方式
- Hibernate_映射_关联关系_一对一映射3_基于主键的方式
- SS6.2_Hibernate_Hibernate_IdStrategyAnnotation 主键生成策略uuid Annotation注解的配置方式
- hibernate一对一双向主键关联_Annotation
- Hibernate关系映射 一对一双向外键关联@OneToOne Annotation方式 双向关联和单向关联的区别
- Hibernate关系映射 一对一双向外键关联@OneToOne Annotation方式
- Hibernate关系映射级别注解(一对一双向外键关联、一对一双向外键联合主键)
- hibernate一对一单向主键关联_Annotation
- [原创]java WEB学习笔记85:Hibernate学习之路-- -映射 一对一关系 ,基于主键方式实现
- Hibernate基于主键和外键的单向一对一的关联映射
- Hibernate一对一关联(共享主键) 注解 || xml
- Hibernate关系映射(一)一对一单向外键关联@OneToOne Annotation方式
- Hibernate注解:一对一主键关联
- Hibernate中,One2Many和Many2One的双向映射关系的基于注解Annotation实现方式
- Hibernate 基于注解的方式 实现的关系映射案例(一对一关系,一对多关系,多对多关系)
- Hibernate的注解方式:多对多双向关系基于外键关联
- hibernate 注解方式一对一 基于外键的形式
- spring与hibernate整合配置基于Annotation注解方式管理实务