一、编写两个实体类

1、一对一关系实现:a:使用外键关联  b:使用主键关联,两个表的主键相同

2、外键方案:配置关联关系:两个实体类互相关联,并且在关联的属性上添加一个@OneToOne代表一个对一个的关系;

              在属性上添加@JoinColumn  nme  存储外键的字段名称  referencedColumnNmae 对方表的字段名称  (可以省略)

3、级联操作: 需要再@OneToOne 注解中添加一个属性 casecade

       CascadeType.PERSIST : 级联添加’  CascadeType.MEGGE : 级联更新’  CascadeType.REMOVE : 级联删除’  CascadeType.ALL : 增删改都用级联操作’

4、主键关联方案:不在使用@JoinColumn注解;使用@PrimaryKeyJoinColumn  注解不需要配置属性;两个实体类关联属性上都要添加

实体类一、
@Entity
@Table(name = "cst_customer")
public class Customer {
// 配置主键自增的策略
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name="cust_id")
private long custId;
@Column(name="cust_name")
private String custName;
@Column(name="cust_source")
private String custSource;
@Column(name="cust_indutry")
private String custIndutry;
@Column(name="cust_level")
private String custLevel;
@Column(name="cust_address")
private String custAddress;
@Column(name="cust_phone")
private String custPhone; //表一对一关系
@OneToOne
//使用外键关联表
@JoinColumn(name = "extid",referencedColumnName = "ext_id")
//使用主键关联表
// @PrimaryKeyJoinColumn
private CustomerExt customerExt; public CustomerExt getCustomerExt() {
return customerExt;
} public void setCustomerExt(CustomerExt customerExt) {
this.customerExt = customerExt;
}
}
实体类二、
@Entity
@Table(name = "cst_customer_ext")
public class CustomerExt {
@Id
@Column(name="ext_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long extId;
private String memo;
private String info; @OneToOne
@JoinColumn(name="custid",referencedColumnName = "cust_id")
// 主键关联
// @PrimaryKeyJoinColumn
private Customer customer; public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
}
}

 

二、编写dao:两个dao都要继承JpaRepository<CustomerExt,Long>

dao一、
public interface CustomerExtDao extends JpaRepository<CustomerExt,Long> {
}
dao二、
public interface CustomerDao extends JpaRepository<Customer,Long> {
}

三、测试

package cn.zrf.jpa;

import cn.zrf.jpa.dao.CustomerDao;
import cn.zrf.jpa.dao.CustomerExtDao;
import cn.zrf.jpa.entity.Customer;
import cn.zrf.jpa.entity.CustomerExt;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class OneToOneTest {
@Autowired
private CustomerDao customerDao;
@Autowired
private CustomerExtDao customerExtDao; @Test
@Transactional
@Commit
public void addCustomer(){
// 1创建Customer 对象
Customer customer = new Customer();
customer.setCustName("张无忌");
customer.setCustAddress("光明顶");
customer.setCustLevel("来了老弟");
// 2创建CustomerExt 对象
CustomerExt customerExt = new CustomerExt();
customerExt.setMemo("我是你哥哥");
customerExt.setInfo("371826");
//3配置对象之间的关联关系
customer.setCustomerExt(customerExt);
customerExt.setCustomer(customer);
// 4 把对象写入数据路
customerDao.save(customer);
customerExtDao.save(customerExt);
}
}

  

springData表关系:一对一的更多相关文章

  1. SpringData表关系:多对多

    一.编写实体类配置关联关系: 1.多对多使用注解@ManyToMany配置:a. 在实体中添加一个集合属性 b.在属性上添加ManyToMany注解 c.@JoinTable 注解配置关联关系(nam ...

  2. springData表关系:一对多

    一.编写实体类进行表关联 1.在一张表的关联属性上添加@OneToMany注解(关联属性用来记录多的一方的信息,是个集合,一般用set) 2.在另一个实体类的关联属性上添加@ManyToOne注解和  ...

  3. djjango models表关系

    *** detail表级联 student表,detail获取student的学生信息,自己做为扩展 一对一关系 models.OneToOneField student = models.OneTo ...

  4. MySQL开发——【多表关系、引擎、外键、三范式】

    多表关系 一对一关系 一对多或多对一关系 多对多关系 MySQL引擎 所谓的MySQL引擎就是数据的存储方式,常用的数据库引擎有以下几种: Myisam与InnoDB引擎之间的区别(面试) ①批量插入 ...

  5. Mysql多表关系

    mysql多表关系 多表关系是关系型数据库特有的 三种关系 一对一关系 一对多关系 多对多关系 总结 一对一 例子:用户和用户信息 外键设置在用户上,外键字段唯一非空 添加 无级联:先增加被关联表记录 ...

  6. MySql 表结构修改、约束条件、表关系

    表结构修改(alter) 查看表的结构:desc 表名; 修改表名:alter table 表名 rename to 新表名; 修改字段名:alter table 表名 change 旧字段名 新字段 ...

  7. mysql之字段的修改,添加、删除,多表关系(外键),单表详细操作(增删改)

    字段的修改.添加和删除 create table tf1( id int primary key auto_increment, x int, y int ); #修改 alter table tf1 ...

  8. MySql 多表关系

    多表关系 一对一关系 一对一关系是最好理解的一种关系,在数据库建表的时候可以将人表的主键放置与身份证表里面,也可以将身份证表的主键放置于人表里面 一对多关系 班级是1端,学生是多端,结合面向对象的思想 ...

  9. Python学习day43-数据库(多表关系)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

随机推荐

  1. 【山外笔记-SVN命令】svnlook命令详解

    本文打印版问文件下载地址 [山外笔记-SVN命令]svnlook命令详解-打印版.pdf 一.命令简介 svnlook是检验Subversion版本库不同方面的命令行工具,不会对版本库有任何修改,只是 ...

  2. Chrome 浏览器安装 ChroPath 插件

    1.下载地址 http://www.cnplugins.com/devtool/chropath/download.html 2.安装方法 a.把下载的文件更改后缀名变为压缩包,然后解压到本地:如下图 ...

  3. [一道蓝鲸安全打卡Web分析] 文件上传引发的二次注入

    蓝鲸打卡的一个 web 文件上传引发二次注入的题解和思考 蓝鲸文件管理系统 源代码地址:http://www.whaledu.com/course/290/task/2848/show 首先在设置文件 ...

  4. selenium 元素定位常用的方法

    元素定位的方法有2个 driver.findElement(By.args) 返回值是WebElement            //此方法是获取单一的页面元素 driver.findElements ...

  5. IO多路复用小故事

    背景故事 小王住在某城市, 生活并长大. 最近, 小城引进了一个企业, 邮局. 这个邮局可了不得, 只要你花上几角钱, 就可以将一封信送到千里之外的朋友手中. 小王也趁机体验了一把, 得劲. 这天, ...

  6. Scala学习系列(三)——入门与基础

    本课程源码共享于 https://github.com/tree1123/learning-scala 首先,打开IDEA编辑器的SbtExampleProject项目,我们将在这个项目下进行练习 本 ...

  7. vagrant + 宝塔 环境搭建遇到的一些问题

    1.js时间戳单位为毫秒,php时间戳为秒. 2.tp里的where()方法如果where("id=".$id)不能用的话就用数组形式的:where(array("id& ...

  8. Makefile: missing separator(did you mean TAB instead of 8 spaces?). Stop.

    通常我们会对vimrc文件加以配置(如将TAB键自动转换为4个空白键). 但正是由于将tab键转换为n个空白键,使得用vim编写的Makefile中不存在tab键(即“\t”)了.恰恰Makefile ...

  9. Scala教程之:Enumeration

    Enumeration应该算是程序语言里面比较通用的一个类型,在scala中也存在这样的类型, 我们看下Enumeration的定义: abstract class Enumeration (init ...

  10. PL/SQL登录Oracle18数据库:ORA-12154:TNS无法解析指定的连接标识符

    PL/SQL登录Oracle18数据库出现ORA-12154:TNS无法解析指定的连接标识符解决以下问题:首先更改Oracle客户端的tnsnames.ora,我的路径是:D:\OracleSQL\n ...