mysql - 插入前删除未完成
问题描述
嗨,我正在使用 Spring Boot hibernate 在 MySQL 中测试记录删除-插入-删除操作,我注意到一个问题,即插入发生时似乎没有完全删除记录。
在 hibernate kickstart 上创建了 3 个初始记录(通过类路径中的 data.sql)
insert into car(id, name, color) values (1, 'Toyota Camry', 'blue');
insert into car(id, name, color) values (2, 'Suzuki Swift', 'yellow');
insert into car(id, name, color) values (3, 'Nissan Qashqai', 'red');
操作是
(1) 创建初始记录
(2) 删除所有记录
(3) 添加新记录
(4) 删除所有记录
进行断言时,发现结果列表仍然包含最后一条记录,因此断言失败。
我想知道这是否是由于在插入发生之前未完成删除操作引起的。
感谢您对此提供一些见解,非常感谢。
模型(Car.java):
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ApiModel(value = "Car", description = "The model for car")
@Schema
@Entity
@Data
public class Car {
@ApiModelProperty(notes = "Car ID.", example = "12345", required = false, position = 0)
@Id
@GeneratedValue
private Long id;
@ApiModelProperty(notes = "Car name.", example = "Suzuki Swift 2020", required = true, position = 1)
@NotNull
@Size(min = 1, max = 30, message = "Name must have length between 1 and 30")
private String name;
@ApiModelProperty(notes = "Car color.", example = "blue", required = true, position = 2)
@NotNull
@Size(min = 1, max = 30, message = "Color must have length between 1 and 30")
private String color;
}
存储库 (CarReposiroty.java)
package com.example.demo.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.model.Car;
@Repository
public interface CarRepository extends JpaRepository<Car, Long> {
}
服务(TestService.java)
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.model.Car;
import com.example.demo.repo.CarRepository;
@Service
public class TestService {
@Autowired
private CarRepository carRepository;
public List<Car> getAllCars() {
return carRepository.findAll();
}
@Transactional
public void addNewCar(Car car) {
carRepository.save(car);
}
@Transactional
public void deleteAllCars() {
carRepository.deleteAll();
}
}
JUnit
...
......
@Test
public void shouldBeAbleToDeleteCarAndAddCarAndGetAllCars() {
testService.deleteAllCars();
Car car = new Car();
car.setName("123456789012345678901234567890");
car.setColor("123456789012345678901234567890");
testService.addNewCar(car);
List<Car> carList = testService.getAllCars();
assertEquals(1, carList.size());
testService.deleteAllCars();
}
...
......
Spring Boot v2.3.1
pom.xml
...
.....
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
...
......
春季休眠配置(application-test.yml)
spring.datasource.url: jdbc:mysql://[the host url]:3306/test
spring.datasource.username: [the username]
spring.datasource.password: [the password]
spring.jpa.hibernate.ddl-auto: create-drop
spring.datasource.initialization-mode: always
MySQL 8.0.17
解决方案
这是因为deleteAllCars()
没有将更改提交到数据库,并且测试方法中的所有操作都包装在单个事务中,因为正在传播事务。在测试类上添加以下注释
@Transactional(propagation = Propagation.NEVER)
还要检查这个相关问题如何在活动的弹簧事务中将数据刷新到数据库中?
推荐阅读
- google-sheets - 需要多少正面调查才能达到 75% 的目标
- xml - 使用父元素在 XSLT 中拆分/分组
- reactjs - 使用打字稿中的反应组件按键动态访问对象
- node.js - 如何在express中的html表中发布数据
- c# - EF Core 插入具有现有子项的新实体
- sqlite - Sqlite FTS4:更改现有表的标记器
- javascript - 如何在本机反应中在获取 POST 请求中抛出 Catch
- visual-studio-code - 切换终端快捷方式在 vscode 中不起作用
- java - JIT 是否将部门优化为一个?
- php - CKFinder 上传图像给出错误“上传完成但有错误”