项目结构:

Wife


package auth.model; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="wife")
public class Wife {
private int id;
private String name;
private int age;
@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


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("wy");
Wife wife=new Wife();
wife.setName("xxt");
wife.setAge(22);
hus.setWife(wife);
husDao.save(hus);
}
public void deleteUser(){
husDao.delete(6);
}
public void updateUser(){
Husband hus=husDao.findOne(7);
hus.setName("ws");
hus.getWife().setName("updatelcy");
husDao.save(hus);
}
public String queryUser(){
Husband husband=husDao.findOne(8);
return JSONObject.toJSONString(husband);
}
}

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

2017-05-25 12:59:21,930 DEBUG [org.hibernate.SQL:109] -
insert
into
husband
(age, name)
values
(?, ?)
2017-05-25 12:59:21,981 DEBUG [org.hibernate.id.IdentifierGeneratorHelper:94] - Natively generated identity: 6
2017-05-25 12:59:21,990 DEBUG [org.hibernate.engine.spi.ActionQueue:196] - Executing identity-insert immediately
2017-05-25 12:59:21,991 DEBUG [org.hibernate.SQL:109] -
insert
into
wife
(age, name)
values
(?, ?)
delete
delete
from
wife
where
id=?
2017-05-25 14:55:37,618 DEBUG [org.hibernate.SQL:109] -
delete
from
husband
where
id=?
update
update
wife
set
age=?,
name=?
where
id=?
2017-05-25 14:46:32,037 DEBUG [org.hibernate.SQL:109] -
update
husband
set
age=?,
name=?
where
id=?
query
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=?
2017-05-25 14:48:24,685 DEBUG [org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl:127] - Starting ResultSet row #0
2017-05-25 14:48:24,687 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-25 14:48:24,705 DEBUG [org.hibernate.engine.internal.TwoPhaseLoad:160] - Resolving associations for [auth.model.Husband#8]
2017-05-25 14:48:24,708 DEBUG [org.hibernate.SQL:109] -
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=?

查询运行结果:

{"age":23,"id":8,"name":"wy","wife":{"age":22,"id":8,"name":"xxt"}}

jpa单向一对一关系外键映射的更多相关文章

  1. jpa双向一对一关联外键映射

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

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

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

  3. Entity Framework - 理清关系 - 基于外键关联的单向一对一关系

      注:本文针对的是 Entity Framework Code First 场景. 之前写过三篇文章试图理清Entity Framework中的一对一关系(单相思(单向一对一), 两情相悦(双向一对 ...

  4. Entity Framework - 基于外键关联的单向一对一关系

    代码的世界,原以为世界关系很简单,确道是关系无处不在.NET世界里ORM框架中EntityFramework作为其中翘楚,大大解放了搬砖工作的重复工作,着实提高了不少生产力,而也碰到过不少问题!比如关 ...

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

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

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

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

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

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

  8. Hibernate之关联关系映射(一对一主键映射和一对一外键映射)

    1:Hibernate的关联关系映射的一对一外键映射: 1.1:第一首先引包,省略 1.2:第二创建实体类: 这里使用用户信息和身份证信息的关系,用户的主键编号既可以做身份证信息的主键又可以做身份证信 ...

  9. Hibernate一对一外键映射

    Hibernate 一对一外键映射                    ------------------------------                            ----- ...

随机推荐

  1. phpexcel表的一些设置

    $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); //set default styles$objPHPE ...

  2. bootstrap学习笔记 插件概述

    Bootstrap插件概览 在前面布局组件章节中所讨论的组件仅仅是个开始.Bootstrap自带的12种jQuery插件,扩展了功能,可以给站点添加更多的互动.即使您不是一名高级的js开发人员, 你也 ...

  3. printf函数对参数的计算顺序

    没想到啊,没想到: printf函数对参数的计算顺序是从右往左的! 我不禁想问一句,这么坑爹的事情,书里居然没有写过.还是我看书不仔细,没有找到?(回头,在自己翻翻那本c语言编程) 于是下面的程序结果 ...

  4. 关于scut在unity上的主动推送

    自带的samples里面,chat的例子涉及主动推送,可作为参考. 在unity里面接收主动推送用Net.CommonCallback 服务端最近的新版本更改了接口,有两种方法推送: ActionFa ...

  5. 让 Vivado有Zybo Board的配置文件

    Zybo Board开发记录: 让 Vivado有Zybo Board的配置文件 由 judyzhong 于 星期三, 07/13/2016 - 10:41 发表 本文转载自:coldnew's bl ...

  6. HTML5之语音识别实例

    HTML5之语音识别实例 代码 <input type="text"  x-webkit-speech id="d1" lang="zh-CN& ...

  7. Java Web应用服务器Resin 国内下载

    在做 PHP On Jvm的测试,发现Resin很难下,速度太慢. 下载地址:http://pan.baidu.com/s/1qWyffnY

  8. Storm学习笔记——安装配置

    1.安装一个zookeeper集群 2.上传storm的安装包,解压 3.修改配置文件conf/storm.yaml #所使用的zookeeper集群主机storm.zookeeper.servers ...

  9. DpQuery.js

    (function (window) { //添加事件的方法通用所有 function addevent(evetname, fn, obj) { if (document.attachEvent) ...

  10. Docker 和一个正常的虚拟机有何区别?

    问: 我多次重读Docker.io文档,希望搞明白Docker.io和一个完全的虚拟机的区别.Docker是如何做到提供一个完整的文件系统,独立的网络环境等等这些功能,同时还没有如此庞大? 为什么部署 ...