赞
踩
- package com.jmj.springDataApp.pojo;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import javax.persistence.*;
-
- @Data
- @Entity
- @Table(name = "tb_student")
- @AllArgsConstructor
- @NoArgsConstructor
- public class Student {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "sid")
- private Long id;
-
- @Column(name = "sname",columnDefinition = "varchar(5)")
- private String name;
-
- @Column(name = "grade",columnDefinition = "int")
- private Integer grade;
-
-
- //单向关联一对一
- /**
- * cascade 设置关联操作
- * ALL, 所有持久化操作
- * PERSIST 只有插入才会执行关联操作
- * MERGE, 只有修改才会执行关联操作
- * REMOVE, 只有删除才会执行关联操作
- * fetch 设置是否懒加载
- * EAGER 立即加载(默认)
- * LAZY 懒加载( 直到用到对象才会进行查询,因为不是所有的关联对象 都需要用到) 必须要加事务注解 通过动态代理的方式代理的,
- * 当通过repository调用完查询方法,session就会关闭,一旦session关闭你就不能查询了
- * orphanRemoval 关联移除(通常在修改的时候会用到)
- * 一旦把关联的数据设置null ,或者修改为其他的关联数据, 如果想删除关联数据, 就可以设置true 设置为null就把账户删除
- * optional 限制关联的对象不能为null
- * true 可以为null(默认 ) false 不能为null
- * mappedBy 将外键约束执行另一方维护(通常在双向关联关系中,会放弃一方的外键约束)
- * 值= 另一方关联属性名
- */
- @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.REMOVE},fetch = FetchType.LAZY,orphanRemoval = true)
- //设置外键的字段名
- @JoinColumn(name = "account_id")
- private Account account;
-
- }
- package com.jmj.springDataApp.pojo;
-
- import lombok.Data;
- import lombok.RequiredArgsConstructor;
-
- import javax.persistence.*;
-
- @Entity
- @Table(name = "tb_account")
- @Data
- public class Account {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String username;
- private String password;
-
-
-
- }
- package com.jmj.springDataApp.mapper;
-
- import com.jmj.springDataApp.pojo.Account;
- import com.jmj.springDataApp.pojo.Student;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.transaction.annotation.Transactional;
-
- import java.util.List;
- import java.util.Optional;
- import java.util.function.Consumer;
-
- @SpringBootTest
- public class OneToOneTest {
-
- @Autowired
- private StudentPageMapper mapper;
-
-
- //插入
- @Test
- void name() {
- Account account = new Account();
- account.setUsername("jmjaccount");
- account.setPassword("123456");
- Student student = new Student();
- student.setName("jmj");
- student.setGrade(1);
- student.setAccount(account);
- //初始化数据
- mapper.save(student);
- }
-
- @Test
- @Transactional(readOnly = true)
- void select() {
- Optional<Student> byId = mapper.findById(55l);
-
- Student student = byId.get();
- student.setName("hkl");
- System.out.println(byId);
- }
-
- @Test
- void delete() {
- mapper.deleteById(55L);
- }
-
- @Test
- void update() {
- Student student = new Student(56L, "jj", 3, null);
- Student save = mapper.save(student);
-
- }
- }
- package com.jmj.springDataApp.pojo;
-
- import lombok.Data;
- import lombok.RequiredArgsConstructor;
-
- import javax.persistence.*;
-
- @Entity
- @Table(name = "tb_account")
- @Data
- public class Account {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String username;
- private String password;
-
- @OneToOne
- @JoinColumn(name = "student_id" )
- private Student student;
-
-
- }
- package com.jmj.springDataApp.pojo;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import javax.persistence.*;
-
- @Data
- @Entity
- @Table(name = "tb_student")
- @AllArgsConstructor
- @NoArgsConstructor
- public class Student {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "sid")
- private Long id;
-
- @Column(name = "sname",columnDefinition = "varchar(5)")
- private String name;
-
- @Column(name = "grade",columnDefinition = "int")
- private Integer grade;
-
-
- //单向关联一对一
- /**
- * cascade 设置关联操作
- * ALL, 所有持久化操作
- * PERSIST 只有插入才会执行关联操作
- * MERGE, 只有修改才会执行关联操作
- * REMOVE, 只有删除才会执行关联操作
- * fetch 设置是否懒加载
- * EAGER 立即加载(默认)
- * LAZY 懒加载( 直到用到对象才会进行查询,因为不是所有的关联对象 都需要用到) 必须要加事务注解 通过动态代理的方式代理的,
- * 当通过repository调用完查询方法,session就会关闭,一旦session关闭你就不能查询了
- * orphanRemoval 关联移除(通常在修改的时候会用到)
- * 一旦把关联的数据设置null ,或者修改为其他的关联数据, 如果想删除关联数据, 就可以设置true 设置为null就把账户删除
- * optional 限制关联的对象不能为null
- * true 可以为null(默认 ) false 不能为null
- * mappedBy 将外键约束指向另一方维护(通常在双向关联关系中,会放弃一方的外键约束)
- * 值= 另一方关联属性名
- */
- @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.REMOVE},fetch = FetchType.LAZY,orphanRemoval = true,optional = false,mappedBy = "student")
- //设置外键的字段名
- @JoinColumn(name = "account_id")
- private Account account;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。