用Spring boot jpa update modify delete 数据和事务管理的那些坑

本文通过在线考试系统的具体实例,详细探讨了Spring Boot应用中使用JPA进行删除操作时遇到的问题及解决方案,包括不同层级事务管理的影响。

有好多文章介绍了Spring boot 博客已经很多了比如说:
http://blog.youkuaiyun.com/linzhiqiang0316/article/details/52639265
http://blog.youkuaiyun.com/linzhiqiang0316/article/details/52638039
http://blog.didispace.com/springbootmultidatasource/等等
上面几个博客都是有关查询的一些例子,但是今天我们的是jpa删除和事务的一些坑
使用的spring boot版本:1.3.7.RELEASE

一.业务场景(在线考试系统)和代码:

业务逻辑:根据问题的id删除答案

repository层:

  int deleteByQuestionId(Integer questionId);

service 层:

public void deleteChoiceAnswerByQuestionId(Integer questionId) {
    choiceAnswerRepository.deleteByQuestionId(questionId);

test(测试)层:

   @Test
    public void testDeleteByQuestionId() {

        choiceAnswerService.deleteChoiceAnswerByQuestionId(5);
        System.out.println("hehehhe");
        System.out.println("hehehhe");

        System.out.println("hehehhe");

        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");


    }

二.问题

问题1:对于modify和delete时如果各层都不加事务管理的话会报这个错误

org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call 

当我们除了query外的modiy和delete如果各层的方法中没有进行事务管理的话(没加@Transactional)话会报错

问题2:只在test层加@Transactional

没有错误但是数据并没有被删除,在用IDEA的调试是,在执行这个测试方法的过程时还可以在choiceanswer表中进行操作并没有加锁事务并没有起作用

问题3:只在 Repository层加@Transactional

 public void deleteChoiceAnswerByQuestionId(Integer questionId) {
        choiceAnswerRepository.deleteByQuestionId(questionId);
        System.out.println("hehehhe");

        System.out.println("hehehhe");
//        questionRepository.delete(5);
        System.out.println("hehehhe");

        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");
    }

这时当执行完
choiceAnswerRepository.deleteByQuestionId(questionId);
数据里面被修改,修改成功

问题4:只在 service层加@Transactional

当只有执行完service内的对应方法时数据才会被删除 (只有在这一层加事务管理才是真正的事务管理)

问题5:在service 层和repository层都加上@transactional

当只有执行完service内的对应方法时数据才会被删除

问题6:只有在test层加上@Transactional,不管service层和repository层加不加@Transactional(可以加可以不加)

数据都不会被删除

问题7:repository层不同注解的执行结果

@Modifying
@Query("delete from ChoiceAnswer c where c.question.id=?1 ")
@Transactional
int deleteByQuestionId(Integer questionId);
与
@Transactional
int deleteByQuestionId(Integer questionId);

有什么区别,上面的会直接执行delete语句
下面的会先执行select 再执行delete
#总结:
事务管理只有在service加上事务管理才起作用,query不需要事务管理但是delete update modify都需要事务管理。为了不在service层不加事务管理可以在repository层的delete update modify加上@transactional 但这样不能真正保持事务的完整性

### Spring BootSpring AI整合实现增删改查功能 在当前的软件生态系统中,Spring Boot 提供了一种更为简洁便捷的方式来构建基于 Spring 的应用程序[^1]。然而,关于 "Spring AI" 的具体定义或项目范围并不明确,目前 Spring 官方并未发布名为 "Spring AI" 的模块。如果用户指的是将人工智能(AI)相关的技术与 Spring Boot 集成,则需要进一步明确具体的 AI 技术栈,例如 TensorFlow、PyTorch 或其他机器学习框架。 以下是一个基于假设场景的示例,展示如何通过 Spring Boot 实现一个简单的增删改查(CRUD)功能,并结合 MyBatis 作为持久层框架[^2]。 #### 1. 引入依赖 首先,在 `pom.xml` 文件中添加必要的依赖项,包括 Spring Boot MyBatis 的集成支持。 ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Spring Boot Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Spring Boot Starter JPA (可选) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> ``` #### 2. 数据库配置 在 `application.properties` 文件中配置数据库连接信息以及 MyBatis 的相关参数。 ```properties # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password # MyBatis 配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.configuration.map-underscore-to-camel-case=true ``` #### 3. 创建实体类 定义一个简单的实体类 `User`,用于表示数据库中的记录。 ```java public class User { private Long id; private String name; private Integer age; // Getters and Setters } ``` #### 4. 创建 Mapper 接口 使用 MyBatis 的 Mapper 接口来定义数据库操作方法。 ```java @Mapper public interface UserMapper { @Insert("INSERT INTO user (name, age) VALUES (#{name}, #{age})") void insertUser(User user); @Delete("DELETE FROM user WHERE id = #{id}") void deleteUser(Long id); @Update("UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}") void updateUser(User user); @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(Long id); } ``` #### 5. 创建服务层 创建一个服务类来封装业务逻辑。 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public void createUser(User user) { userMapper.insertUser(user); } public void removeUser(Long id) { userMapper.deleteUser(id); } public void modifyUser(User user) { userMapper.updateUser(user); } public User fetchUser(Long id) { return userMapper.getUserById(id); } } ``` #### 6. 创建控制器 最后,创建一个 REST 控制器来暴露 CRUD 操作的接口。 ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @PostMapping public ResponseEntity<String> createUser(@RequestBody User user) { userService.createUser(user); return ResponseEntity.ok("User created successfully."); } @DeleteMapping("/{id}") public ResponseEntity<String> deleteUser(@PathVariable Long id) { userService.removeUser(id); return ResponseEntity.ok("User deleted successfully."); } @PutMapping public ResponseEntity<String> updateUser(@RequestBody User user) { userService.modifyUser(user); return ResponseEntity.ok("User updated successfully."); } @GetMapping("/{id}") public ResponseEntity<User> getUser(@PathVariable Long id) { User user = userService.fetchUser(id); if (user == null) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok(user); } } ``` ### 注意事项 - 上述代码示例仅展示了一个基本的 Spring Boot 与 MyBatis 集成的 CRUD 功能实现[^2]。 - 如果涉及 "Spring AI" 的具体需求,请提供更详细的上下文或说明,以便进一步调整解决方案。 - 在实际开发中,建议对 SQL 语句进行优化,并合理配置连接池缓存策略以提高性能[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值