Spring Data JPA的一对一、LazyInitializationException异常、一对多、多对多操作

 Spring Data JPA系列

1、SpringBoot集成JPA及基本使用

2、Spring Data JPA Criteria查询、部分字段查询

3、Spring Data JPA数据批量插入、批量更新真的用对了吗

4、Spring Data JPA的一对一、LazyInitializationException异常、一对多、多对多操作

前言

通过前三篇Spring Data JPA的博文,相信大家对JPA有了一定的了解,然而前面的文章都只介绍单个表,这一篇,将给大家分享一下多表关系的定义及相关操作。在开始介绍之前,先了解一些理论知识。

CascadeType

JPA框架中的cascade属性用于指定实体之间的级联操作。级联操作是指当一个实体的状态发生改变时,关联的其他实体是否同时发生改变。简单理解:cascade用于设置当前实体是否能够操作关联的另一个实体的权限。

cascade的取值为CascadeType枚举类型

1)CascadeType.PERSIST:持久化操作时会级联执行(cascade是用于设置实体的权限,所以对应的持久化操作也是针对实体对象的持久化,对应EntityManager的persist()方法操作,而如果是通过JPQL、HQL或Sql实现的持久化,则不会级联。下同。);

2)CascadeType.MERGE:合并(更新)操作时级联执行;

3)CascadeType.REMOVE:删除操作时级联执行;

4)CascadeType.REFRESH:刷新操作时级联执行;

5)CascadeType.DETACH:级联脱管/游离操作,如果要删除一个实体,但是它有外键无法删除,需要这个级联权限。它会撤销所有相关的外键关联;

6)CascadeType.ALL:上面的5种操作都会级联执行;

当通过注解来映射持久化类时,如果希望使用底层Hibernate的一些级联特性,还可以使用CascadeType类的一些常量,例如:

1)org.hibernate.annotations.CascadeType.LOCK:当通过底层Session的lock()方法把当前游离对象加入到持久化缓存中时,会把所有关联的游离对象也加入到持久化缓存中;

2)org.hibernate.annotations.CascadeType.REPLICATE:当通过底层Session的replicate()方法复制当前对象时,会级联复制所有关联的对象;

3)org.hibernate.annotations.CascadeType.SAVE_UPDATE:当通过底层Session的save()、update()及saveOrUpdate()方法来保存或更新当前对象时,会级联保存所有关联的新建的临时对象,并且级联更新所有关联的游离对象;

注:在实际开发中谨慎使用cascade属性,以免对数据库造成不可预知的影响。

一对一

一对一关系,一个表中的记录与另一个表中的记录之间存在唯一的对应关系。以商品为例,一件商品只有一个详情信息。

2.1 实体类

package com.jingai.jpa.dao.entity;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;import javax.persistence.*;
import java.util.Date;@Data
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
@Table(name = "tb_goods")
public class GoodsEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String subtitle;private Long classificationId;private Date createTime;@Transientprivate String createTimeStr;@OneToOne(cascade = {CascadeType.MERGE, CascadeType.REMOVE})@JoinColumn(name = "id", referencedColumnName = "id")private GoodsDetailEntity detail;}

@Transient注解:该注解用于标注对应的属性不在数据库表中;

@OneToOne注解:用于实体上的注解,表示一对一关系。此处商品详情的数据变更同商品并存,此处的cascade可以设置CascadeType.MERGE和CascadeType.REMOVE;

@JoinColumn注解:标注实体类与数据库的对应关系。主要可选属性如下:

1)name:定义被标注属性在数据库表中所对应的字段的名称;

2)unique:定义被标注属性在数据库表中的值是否唯一,默认为false;

3)insertable:表示在使用“Insert”脚本插入数据时,是否需要插入被标注属性的值,默认为true;

4)updatable:表示在使用“Update”脚本插入数据时,是否需要更新被标注属性的值,默认为true;

5)referencedColumnName:定义所关联表中的字段名;

6)table:定义包含当前字段的表名;

package com.jingai.jpa.dao.entity;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;import javax.persistence.*;@Data
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
@Table(name = "tb_goods_detail")
public class GoodsDetailEntity {@Idprivate Long id;private String detailDescribe;private String pictures;}

2.2 Repository类

package com.jingai.jpa.dao;import com.jingai.jpa.dao.entity.GoodsEntity;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;public interface GoodsRepository extends JpaRepositoryImplementation<GoodsEntity, Long> {
}
package com.jingai.jpa.dao;import com.jingai.jpa.dao.entity.GoodsDetailEntity;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;public interface GoodsDetailRepository extends JpaRepositoryImplementation<GoodsDetailEntity, Long> {
}

2.3 Service类

package com.jingai.jpa.service;import com.jingai.jpa.dao.GoodsDetailRepository;
import com.jingai.jpa.dao.GoodsRepository;
import com.jingai.jpa.dao.entity.GoodsDetailEntity;
import com.jingai.jpa.dao.entity.GoodsEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;@Service
public class GoodsService {@Resourceprivate GoodsRepository goodsRepository;@Resourceprivate GoodsDetailRepository goodsDetailRepository;@Transactionalpublic GoodsEntity save(GoodsEntity entity, GoodsDetailEntity detail) {entity.setCreateTime(new Date());entity = goodsRepository.save(entity);detail.setId(entity.getId());detail = goodsDetailRepository.save(detail);entity.setDetail(detail);return entity;}public GoodsEntity get(long id) {// getById()使用懒加载的方式访问数据库,只有在真正访问GoodsEntity的才会真正执行数据库访问return goodsRepository.getById(id);}public GoodsEntity find(long id) {// findById()是立即访问数据库查询数据Optional<GoodsEntity> entity = goodsRepository.findById(id);return entity.isPresent() ? entity.get() : null;}/*** 修改。由于在GoodsEntity中的GoodsDetailEntity的@OneToOne注解配置了CascadeType.MERGE,修改更新会级联执行*/@Transactionalpublic GoodsEntity update(GoodsEntity goods, GoodsDetailEntity detail) {detail.setId(goods.getId());goods.setDetail(detail);goods.setCreateTime(new Date());goods = goodsRepository.save(goods);return goods;}}

说明:此处为了讲解方便,不先定义接口后定义实现类。

2.4 Controller类

package com.jingai.jpa.controller;import com.jingai.jpa.dao.entity.GoodsDetailEntity;
import com.jingai.jpa.dao.entity.GoodsEntity;
import com.jingai.jpa.service.GoodsService;
import com.jingai.jpa.util.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Map;@RestController
@RequestMapping("goods")
public class GoodsController {@Resourceprivate GoodsService goodsService;@PostMapping("save")public Map<String, Object> save(GoodsEntity goods, GoodsDetailEntity detail) {goods = goodsService.save(goods, detail);goods.setCreateTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goods.getCreateTime()));return ResponseUtil.success(goods);}@GetMapping("get")public Map<String, Object> get(long id) {GoodsEntity entity = goodsService.get(id);return ResponseUtil.success(entity);}@GetMapping("find")public Map<String, Object> find(long id) {GoodsEntity entity = goodsService.find(id);return ResponseUtil.success(entity);}@PostMapping("update")public Map<String, Object> update(GoodsEntity goods, GoodsDetailEntity detail) {goods = goodsService.update(goods, detail);goods.setCreateTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goods.getCreateTime()));return ResponseUtil.success(goods);}}

2.5 访问接口,结果如下:

2.6 LazyInitializationException异常

当访问

http://localhost:8080/goods/get?id=14

时,系统会报Method threw ‘org.hibernate.LazyInitializationException‘ exception. Cannot evaluate异常,原因是JpaRepository.getById()方法是懒加载,访问该方法时,返回一个对应实体的引用,而该引用是没有值的,此时创建了一个临时的session,并没有真正访问数据库,并立即关闭了session。在Controller层中访问该引用的信息时才真正执行数据库的访问,此时session已关闭,所以报了上面的异常,提示no session。

解决方法:

方法一:在application.yml中添加spring.jpa.open-in-view=true。这个配置在SpringBoot集成JPA及基本使用-CSDN博客中有讲解,建议关闭。而且该配置只能解决通过controller层访问引起的懒加载问题;

方法二:在application.yml中添加spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true。该方法既能解决通过Controller层访问引起的懒加载,也能解决定时任务等访问引起的懒加载问题;

当然也可以使用CrudRepository.findById()这个接口,立即访问数据库。

一对多

一对多关系,一个表中的一条记录与另一个表中的多条记录之间相对应。如一个会员有多个地址。

3.1 实体类

package com.jingai.jpa.dao.entity;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;import javax.persistence.*;
import java.util.Date;@Data
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
@Table(name = "tb_member")
public class MemberEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private int id;private String name;private String sex;private Date createTime;
}
package com.jingai.jpa.dao.entity;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;import javax.persistence.*;
import java.util.Date;@Data
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
@Table(name = "tb_address")
public class AddressEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private int id;@Column(name = "member_id")private int memberId;@ManyToOne@JoinColumn(name = "member_id", referencedColumnName = "id", updatable = false, insertable = false)private MemberEntity member;private String province;private String city;private String address;private String phone;private Date createTime;
}

@ManyToOne注解:地址与会员是多对一的关系,所以此处添加了@ManyToOne注解,表示多对一,不添加cascade配置,因为会员也可以没有地址,它们之前没有必然关联;

@JoinColumn注解:此处的updateable为false,insertable也为false。即对Address表的修改不会影响到Member表;

3.2 Repository类

package com.jingai.jpa.dao;import com.jingai.jpa.dao.entity.MemberEntity;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;public interface MemberRepository extends JpaRepositoryImplementation<MemberEntity, Long> {
}
package com.jingai.jpa.dao;import com.jingai.jpa.dao.entity.AddressEntity;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;public interface AddressRepository extends JpaRepositoryImplementation<AddressEntity, Long> {
}

3.3 Service类

package com.jingai.jpa.service;import com.jingai.jpa.common.form.AddressForm;
import com.jingai.jpa.dao.AddressRepository;
import com.jingai.jpa.dao.entity.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;@Service
public class AddressService {@Resourceprivate AddressRepository addressRepository;public Page<AddressEntity> listByPage(AddressForm form) {// 创建一个Specification,实现接口中的toPredicate()方法,该方法返回一个PredicateSpecification<AddressEntity> specification = new Specification<AddressEntity>() {@Overridepublic Predicate toPredicate(Root<AddressEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {List<Predicate> predicates = new ArrayList<>(8);// 通过会员名称查询if(StringUtils.hasText(form.getName())) {// 先通过AddressEntity_.member定位到MemberEntity,然后再定位到MemberEntity_.namepredicates.add(criteriaBuilder.like(root.get(AddressEntity_.member).get(MemberEntity_.name), "%" + form.getName() + "%"));}if(StringUtils.hasText(form.getPhone())) {predicates.add(criteriaBuilder.like(root.get(AddressEntity_.PHONE), "%" + form.getPhone() + "%"));}if(form.getStartDate() != null) {predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(AddressEntity_.createTime), form.getStartDate()));}if(form.getEndDate() != null) {predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(AddressEntity_.createTime), form.getEndDate()));}return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();}};// 创建排序字段,可设置多个Sort sort = Sort.by(Sort.Direction.DESC, ProductEntity_.createTime.getName());Pageable pageable = PageRequest.of(form.getPageIndex(), form.getPageSize(), sort);// 使用JpaSpecificationExecutor的findAll()方法,只能返回实体类的集合return addressRepository.findAll(specification, pageable);}}

此处重点讲解AddressService中使用Criteria查询某个会员名称的地址信息。对Criteria查询不清楚的,可以看

Spring Data JPA Criteria查询、部分字段查询-CSDN博客

示例中的AddressForm在该篇博文也有讲解到。

此处可以通过JPA的元模式,很方便的实现表的关联查询。

3.4 Controller类

@RestController
@RequestMapping("address")
public class AddressController {@Resourceprivate AddressService addressService;@GetMapping("search")public Map<String, Object> search(AddressForm form) {return ResponseUtil.success(addressService.listByPage(form));}}

访问接口后显示效果如下:

多对多

多对多关系,指两个表中的记录可以相互对应。如一个学生可以选择多门课程,一门课程可以被多个学生选择。针对多对多关系的场景,通常使用中间表进行关联。

@Data
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
@Table(name = "tb_student")
public class Student {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;@ManyToMany@JoinTable(name = "student_course", joinColumns = {@JoinColumn(name = "student_id")}, inverseJoinColumns = {@JoinColumn(name = "course_id")})private List<Course> courses;}
@Data
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
@Table(name = "tb_student")
public class Course {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;@ManyToMany(mappedBy = "courses")private List<Student> students;}

@ManyToMany注解:用于标注多对多关系。mappedBy为被注解的多的实体类的属性字段;

@JoinTable注解:name为关联表的名称;joinColumns:关联student表的id;inverseJoinColumns:关联Course表的id;

结尾

限于篇幅,Spring Data JPA的一对一、一对多、多对多操作就分享到这里。

关于本篇内容你有什么自己的想法或独到见解,欢迎在评论区一起交流探讨下吧

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/832288.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[SaaS]建筑领域的sd应用

AirchiDesignhttp://www.aiarchi.art/#/建筑学长——千万建筑师的资源库和AI绘图创作平台建筑学长官网,为青年设计师建立的线上资源共享及AI绘图创作渲染平台,免费提供海量设计案例、CAD图纸、SU模型、PS素材、软件插件下载,提供丰富的设计软件教学与灵感参考素材图库。https:/…

扩展学习|一文读懂知识图谱

一、知识图谱的技术实现流程及相关应用 文献来源&#xff1a;曹倩,赵一鸣.知识图谱的技术实现流程及相关应用[J].情报理论与实践,2015, 38(12):127-132. &#xff08;一&#xff09;知识图谱的特征及功能 知识图谱是为了适应新的网络信息环境而产生的一种语义知识组织和服务的方…

常用六大加密软件排行榜|好用加密文件软件分享

为了保障数据安全&#xff0c;越来越多的企业开始使用文件加密软件。哪款加密软件适合企业哪些办公场景呢&#xff1f; 今天就给大家推荐一下文件加密软件排行榜的前六名&#xff1a; 1.域智盾 这款软件专为企业和政府机构设计&#xff0c;提供全面的文件保护解决方案。 点…

SOLIDWORKS Electrical电气智能零部件的运用

电气2D向电气3D转型&#xff0c;3D模型无疑是重中之重&#xff0c;精准、正确的3D模型有利于电线长度、空间大小、耗材的计算。而线槽、导轨因为要根据实际情况裁剪&#xff0c;所以即使同一规格的线槽、导轨&#xff0c;在装配时也得根据实际情况&#xff0c;修改长度&#xf…

一文带你了解多数企业系统都在用的 RBAC 权限管理策略

前言 哈喽你好呀&#xff0c;我是 嘟老板&#xff0c;今天我们来聊聊几乎所有企业系统都离不开的 权限管理&#xff0c;大家平时在做项目开发的时候&#xff0c;有没有留意过权限这块的设计呢&#xff1f;都是怎样实现的呢&#xff1f;如果现在脑子里对于这块儿不够清晰&#…

【爬虫】爬取A股数据写入数据库(一)

1. 对东方财富官网的分析 步骤&#xff1a; 通过刷新网页&#xff0c;点击等操作&#xff0c;我们发现https://datacenter-web.eastmoney.com/api/data/v1/get?请求后面带着一些参数即可以获取到相应数据。我们使用python来模拟这个请求即可。 我们以如下选择的页面为切入点…

经典的设计模式和Python示例(一)

目录 一、工厂模式&#xff08;Factory Pattern&#xff09; 二、单例模式&#xff08;Singleton Pattern&#xff09; 三、观察者模式&#xff08;Observer Pattern&#xff09; 一、工厂模式&#xff08;Factory Pattern&#xff09; 工厂模式&#xff08;Factory Pattern…

项目|保障房房产管理系统,政务房产解决方案

一、系统概况 保障房管理系统是是为了落实中央关于住房保障的相关政策&#xff0c;实现对低收入家庭住房状况的调查管理、保障计划及落实管理、保障申请及审核管理、保障户和保障房源档案管理等。 针对政府保障房产管理的一站式解决方案&#xff0c;专注于为解决复杂、繁琐的…

【STM32嵌入式系统设计与开发】——18DAC(DAC输出应用)

这里写目录标题 STM32资料包&#xff1a; 百度网盘下载链接&#xff1a;链接&#xff1a;https://pan.baidu.com/s/1mWx9Asaipk-2z9HY17wYXQ?pwd8888 提取码&#xff1a;8888 一、任务描述二、任务实施1、工程文件夹创建2、函数编辑&#xff08;1&#xff09;主函数编辑&#…

Eclipse 安装 lombok 和配置

如 Eclipse 不配置的话&#xff0c;是没有办法编译 lombok 项目的。 下载 Jar 如果你项目使用的是 maven&#xff0c;那么 jar 应该下载下来了。 到 pom.xm 文件中找到有关 lombok 的依赖。 <dependency><groupId>org.projectlombok</groupId><artifac…

[含1-4问完整代码]2024深圳杯数模D题成品论文42页word版

基于有限元分析的音板振动模态分析与参数识别 2024深圳杯D题42页成品论文1-4小问完整代码高清结果图https://www.jdmm.cc/file/2710609 摘 要 本文针对音板振动建模与参数识别的一系列问题,采用了多种数学建模方法和求解算法,对相关问题进行了深入分析和求解。问题1的 Kirch…

unity滑动地图上气泡随之移动旋转

前言 最近在做世界地图,需要实现一个气泡提示的功能。大概描述:地图上可上下左右滑动,地图上有若干个资源点,玩家最多可开采1个资源点 当玩家有正在开采的资源点时,需要在资源点上方显示带有方向的气泡提示,当资源点滑动到 显示屏幕外时,气泡需要在可视屏幕边缘,且指向…

节能洗车房车牌识别项目实战

项目背景 学电子信息的你加入了一家节能环保企业&#xff0c;公司的主营产品是节能型洗车房。由于节水节电而且可自动洗车&#xff0c;产品迅速得到了市场和资本的认可。公司决定继续投入研发新一代产品&#xff1a;在节能洗车房的基础上实现无人值守的功能。新产品需要通过图…

vue3—项目创建

背景 初次学习vue3&#xff0c;需要从项目创建开始。 步骤 打开cmd命令行&#xff0c;进入项目存放目录下&#xff0c;执行创建命令&#xff1a; npm create vuelatest 这一指令将会安装并执行 create-vue&#xff0c;它是 Vue 官方的项目脚手架工具。你将会看到一些诸如 …

IDEA远程连接Docker服务

1.确保你的服务器已经安装docker docker安装步骤可查看&#xff1a;CentOS 9 (stream) 安装 Docker 2.安装完docker后开启远程连接 默认配置下&#xff0c;Docker daemon只能响应来自本地Host的客户端请求。如果要允许远程客户端请求&#xff0c;需要在配置文件中打开TCP监听…

vue2 webpack-dev-server Unknown promise rejection reason

在vue.config.js中添加如下配置&#xff0c;重启项目即可 module.exports defineConfig({devServer: {client: {overlay: false,},} })参考

手拉手springboot整合kafka

前期准备安装kafka 启动Kafka本地环境需Java 8以上 Kafka是一种高吞吐量的分布式发布订阅消息系统&#xff0c;它可以处理消费者在网站中的所有动作流数据。 Kafka启动方式有Zookeeper和Kraft&#xff0c;两种方式只能选择其中一种启动&#xff0c;不能同时使用。 Kafka下载…

PHP定时任务框架taskPHP3.0学习记录7宝塔面板手动可以执行自动无法执行问题排查及解决方案(sh脚本、删除超过特定天数的日志文件、kill -9)

PHP定时任务框架taskPHP3.0学习记录 PHP定时任务框架taskPHP3.0学习记录1&#xff08;TaskPHP、执行任务类的实操代码实例&#xff09;PHP定时任务框架taskPHP3.0学习记录2&#xff08;环境要求、配置Redis、crontab执行时间语法、命令操作以及Screen全屏窗口管理器&#xff0…

深入剖析Tomcat(六) Tomcat各组件的生命周期控制

Catalina中有很多组件&#xff0c;像上一章提到的四种容器&#xff0c;载入器&#xff0c;映射器等都是一种组件。每个组件在对外提供服务之前都需要有个启动过程&#xff1b;组件在销毁之前&#xff0c;也需要有个关闭过程&#xff1b;例如servlet容器关闭时&#xff0c;需要调…

字符串循环左移

#include <iostream> #include <string> using namespace std;int main() {string s1, s2;getline(cin, s1);int n;cin >> n;if(n>s1.size()){nn-s1.size();s2 s1.substr(0, n);s1.erase(0, n);cout << s1s2;}else{// 提取s1的前n个字符到s2中s2 …