jpa单向一对一关系外键映射
项目结构:
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单向一对一关系外键映射的更多相关文章
- jpa双向一对一关联外键映射
项目结构: Wife package auth.model; import javax.persistence.CascadeType; import javax.persistence.Column ...
- Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射
Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射 Hibernate提供了两 ...
- Entity Framework - 理清关系 - 基于外键关联的单向一对一关系
注:本文针对的是 Entity Framework Code First 场景. 之前写过三篇文章试图理清Entity Framework中的一对一关系(单相思(单向一对一), 两情相悦(双向一对 ...
- Entity Framework - 基于外键关联的单向一对一关系
代码的世界,原以为世界关系很简单,确道是关系无处不在.NET世界里ORM框架中EntityFramework作为其中翘楚,大大解放了搬砖工作的重复工作,着实提高了不少生产力,而也碰到过不少问题!比如关 ...
- 011一对一 唯一外键关联映射_单向(one-to-one)
² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...
- JPA 系列教程9-双向一对一唯一外键
双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...
- 012一对一 唯一外键关联映射_双向(one-to-one)
² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...
- Hibernate之关联关系映射(一对一主键映射和一对一外键映射)
1:Hibernate的关联关系映射的一对一外键映射: 1.1:第一首先引包,省略 1.2:第二创建实体类: 这里使用用户信息和身份证信息的关系,用户的主键编号既可以做身份证信息的主键又可以做身份证信 ...
- Hibernate一对一外键映射
Hibernate 一对一外键映射 ------------------------------ ----- ...
随机推荐
- CentOS源码编译安装Nginx
安装编译用到的软件: yum install glib2-devel openssl-devel pcre-devel bzip2-devel gzip-devel 现在到http://nginx.o ...
- discuz 安装 文件不可写
discuz安装过程中,系统会自动检查环境及文件目录权限,当出现目录不可写: 这个时候只需要用ftp修改文件夹权限就可以了
- Sql學習資源
http://blog.csdn.net/wltica/article/category/1324738/1 SQL Server 整体方案系列: 1. SQL Server 数据归档方案 2. SQ ...
- 【转载】使用Exp和Expdp导出数据的性能对比与优化
转自:http://blog.itpub.net/117319/viewspace-1410931/ 序:这方面的文章虽然很多人写过,但是结合实际进行详细的对比分析的不多,这里,结合所在公司的行业,进 ...
- Atitti.数据操作crud js sdk dataServiceV3设计说明
Atitti.数据操作crud js sdk dataServiceV3设计说明 1. 增加数据1 1.1. 参数哦说明1 2. 查询数据1 2.1. 参数说明2 3. 更新数据2 3.1. 参数说明 ...
- PHPCMS模块开发相关文件
名称 类型说明 Guestbook 模块后台控制器 index 模块前台控制器 Install 安装文件夹 ├templates ├config.inc.php ├extention.inc.php ...
- 使用 OpCache 提升 PHP 性能
使用 OpCache 提升 PHP 性能 wdd2007wdd2007 .6k 1月29日 发布 推荐 推荐 收藏 收藏,.3k 浏览 OpCache 通过对 opcode 的缓存和优化来提升 PHP ...
- 关于mysql内存表的一个帖子(转载)
地址:http://bbs.csdn.net/topics/360030699 引用楼主 zgycsmb 的回复:问:1mysql的内存表性能怎么样2mysql的内存表与system v 这种共享内存 ...
- CCNA2.0笔记_STP
STP介绍 STP的主要任务是阻止在第二层网络(网桥或交换机)上产生网络环路(通过将特定的端口选为 Blocking state),来实现无环的拓扑 ; STP交换机之间使用Trunk连接 ; Cis ...
- Mysql下Union注入Bypass安全狗过程
文章转载于:http://www.0aa.me/index.php/archives/95/ 一次众测发现个注入,然后有安全狗就顺带看了下安全狗. 先fuzz看看安全狗拦截什么关键词union sel ...