项目结构:

Wife


package auth.model; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table; @Entity
@Table(name="wife")
public class Wife {
private int id;
private String name;
private int age;
private Husband husband;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//mappedBy="wife"表示Husband中wife属性已经做映射,如果不写mappedBy="wife"会在wife表中生成一个husband_id外键,这是多余的。
//mappedBy指向被维护端wife,由husband维护
@OneToOne(mappedBy="wife",cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=Husband.class)
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}

Husband


package auth.model; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table; @Entity
@Table(name="husband")
public class Husband {
private int id;
private String name;
private int age;
private Wife wife;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//注意:这个husband的id要和wife的id一样,表的主键同时是另一张表的外键,不然无法关联
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=Wife.class)
@JoinColumn(name="id", unique=true, nullable=false, updatable=false)
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}

DAO:


package auth.dao; import org.springframework.data.jpa.repository.JpaRepository; import auth.model.Husband; public interface HusbandRepository extends JpaRepository<Husband, Integer>{
} package auth.dao; import org.springframework.data.jpa.repository.JpaRepository; import auth.model.Wife; public interface WifeRepository extends JpaRepository<Wife, Integer>{ }

UserController


package auth.controller; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; import auth.dao.HusbandRepository;
import auth.dao.WifeRepository;
import auth.model.Husband;
import auth.model.Wife;
@Service
public class UserController {
private Logger log=LoggerFactory.getLogger(this.getClass());
@Autowired
private HusbandRepository husDao;
@Autowired
private WifeRepository wifeDao;
public void addUser(){
Husband hus=new Husband();
hus.setAge(23);
hus.setName("wy2");
Wife wife=new Wife();
wife.setName("xxt2");
wife.setAge(22);
wife.setHusband(hus);
wifeDao.save(wife);
}
public void deleteUser(){
wifeDao.delete(7);
}
public void updateUser(){
Wife wife=wifeDao.findOne(8);
wife.setName("wf");
wife.getHusband().setName("updatehus");
wifeDao.save(wife);
}
public String queryUser(){
Wife wife=wifeDao.findOne(8);
return JSONObject.toJSONString(wife);
}
}

Tests


package spring; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import auth.controller.UserController; public class Tests extends Base{
@Autowired
private UserController con;
@Test
public void addUser(){
con.addUser();
}
@Test
public void deleteUser(){
con.deleteUser();
}
@Test
public void updateUser(){
con.updateUser();
}
@Test
public void queryUser(){
String result=con.queryUser();
System.out.println(result);
} }

运行过程:

add

insert
into
wife
(age, name)
values
(?, ?)
2017-05-26 09:01:21,653 DEBUG [org.hibernate.id.IdentifierGeneratorHelper:94] - Natively generated identity: 9
2017-05-26 09:01:21,664 DEBUG [org.hibernate.engine.spi.ActionQueue:196] - Executing identity-insert immediately
2017-05-26 09:01:21,665 DEBUG [org.hibernate.SQL:109] -
insert
into
husband
(age, name)
values
(?, ?)

delete

delete
from
husband
where
id=?
2017-05-26 08:58:00,180 DEBUG [org.hibernate.SQL:109] -
delete
from
wife
where
id=?

update

update
husband
set
age=?,
name=?
where
id=?
2017-05-26 09:10:33,317 DEBUG [org.hibernate.SQL:109] -
update
wife
set
age=?,
name=?
where
id=?

query

select
wife0_.id as id1_1_0_,
wife0_.age as age2_1_0_,
wife0_.name as name3_1_0_
from
wife wife0_
where
wife0_.id=?
2017-05-26 09:00:05,215 DEBUG [org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl:127] - Starting ResultSet row #0
2017-05-26 09:00:05,219 DEBUG [org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl:142] - On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
2017-05-26 09:00:05,241 DEBUG [org.hibernate.engine.internal.TwoPhaseLoad:160] - Resolving associations for [auth.model.Wife#8]
2017-05-26 09:00:05,243 DEBUG [org.hibernate.SQL:109] -
select
husband0_.id as id1_0_0_,
husband0_.age as age2_0_0_,
husband0_.name as name3_0_0_
from
husband husband0_
where
husband0_.id=?

jpa双向一对一关联外键映射的更多相关文章

  1. jpa单向一对一关系外键映射

    项目结构: Wife package auth.model; import javax.persistence.Column; import javax.persistence.Entity; imp ...

  2. Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射

                                                        Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射 Hibernate提供了两 ...

  3. JPA 系列教程9-双向一对一唯一外键

    双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...

  4. 012一对一 唯一外键关联映射_双向(one-to-one)

    ²  两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²  有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...

  5. java之hibernate之基于外键的双向一对一关联映射

    这篇讲解 基于外键的双向一对一关联映射 1.考察如下信息,人和身份证之间是一个一对一的关系.表的设计 2.类结构 Person.java public class Person implements ...

  6. java之hibernate之基于主键的双向一对一关联映射

    这篇 基于主键的双向一对一关联映射 1.依然考察人和身份证的一对一关系,如果采用主键关联,那么其表结构为: 2.类结构 Person.java public class Person implemen ...

  7. 011一对一 唯一外键关联映射_单向(one-to-one)

    ²  两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²  有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...

  8. Hibernate 再接触 关系映射 一对一单向外键关联

    对象之间的关系 数据库之间的关系只有外键 注意说关系的时候一定要反面也要说通 CRUD 数据库之间设计 主键关联 单向的外键关联 中间表 一对一单向外键关联 Husband.java package ...

  9. Hibernate一对一单向外键关联

    一.一对一单向外键关联: 一对一单向外键关联主要用到了以下两个注解: 1.OneToOne(cascade=CasecadeTYPE.ALL); cascade=CasecadeTYPE.ALL:表示 ...

随机推荐

  1. Python 的__name__属性

    Python 的__name__属性: 一个模块被另一个程序第一次引入时,其主程序将运行. 如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身 ...

  2. 改动文件后缀的C语言实现

    ,其他配置项保持一致.         step 3: 在"Old2New"目录下新建名为"update.bat"的批处理文件,该文件的内容为: ChangeS ...

  3. Node.js搭建本地web服务(转)

    http://www.cnblogs.com/wangfupeng1988/p/4143996.html https://github.com/finderL/webserver

  4. 经典.net面试题目(转载)

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. protected : 保 ...

  5. unity, GetComponent<Renderer>().bounds.size vs GetComponent<MeshFilter>().sharedMesh.bounds.size

    GetComponent<MeshFilter>().sharedMesh.bounds.size获得的是未经缩放的大小. GetComponent<Renderer>().b ...

  6. Sql學習資源

    http://blog.csdn.net/wltica/article/category/1324738/1 SQL Server 整体方案系列: 1. SQL Server 数据归档方案 2. SQ ...

  7. 通过 SysVinit、Systemd 和 Upstart 管理系统自启动进程和服务

    管理 Linux 自启动进程 Linux 系统的启动程序包括多个阶段,每个阶段由一个不同的图示块表示.下面的图示简要总结了启动过程以及所有包括的主要组件. Linux 启动过程 当你按下你机器上的电源 ...

  8. android studio - 修改logcat颜色字体

     修改logcat 颜色 (1) 在Android Studio 菜单栏 打开 Preferences -> Editer -> Colors & Fonts -> Andr ...

  9. erlang supervisor simple_one_for_one实例

    simple_one_for_one vs one_for_one: 相同点: 这种Restart Strategy和one_for_one基本相同(即当一个child process挂掉后,仅仅重启 ...

  10. C#通过SFTP协议操作文件

    本文主要是C#调用SSH实现文件上传下载功能,主要是要引用第三方类库Tamir.SharpSSH.dll. 以下是SFTPHelper类,实现了对文件的操作,可供参考. public class SF ...