Spring DBsetup 后数据不增加
Spring data doesn't increment after DBsetup
我有这样的疑问。我正在使用 DBsetup 进行 spring 启动测试和 postgresql 数据库。我正在使用 DBsetup 设置用户,但是当我试图通过 spring 数据设置另一个用户时,我遇到了下一个异常:
Подробности: Key (id)=(1) already exists.
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [users_pkey];
这是我的测试class:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("/application-test.properties")
public class UserRepositoryTest {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private UserRepository userRepository;
@Autowired
private DataSource dataSource;
@Before
public void insertData() throws SQLException {
Operation operation = sequenceOf(CommonOperations.DELETE_ALL, CommonOperations.INSERT_USER);
DbSetup dbSetup = new DbSetup(new DataSourceDestination(dataSource), operation);
dbSetup.launch();
}
@After
public void cleanPK() throws SQLException {
DBUtil.resetAutoIncrementColumns(applicationContext, "user");
}
@Test
public void registerUser() {
val user = new User(null, "Glass", "123123", "glass999@mail.ru");
assertEquals(user, userRepository.saveAndFlush(user));
}
}
数据库设置操作:
public class CommonOperations {
public static final Operation DELETE_ALL = deleteAllFrom("article_tag", "article", "tag", "users");
public static final Operation INSERT_USER =
insertInto("users")
.columns("id", "email", "password", "username")
.values(1, "krikkk998@mail.ru", "123123", "Daimon")
.build();
}
Class 重置序列:
@NoArgsConstructor
public final class DBUtil {
public static void resetAutoIncrementColumns(ApplicationContext applicationContext,
String... tableNames) throws SQLException {
DataSource dataSource = applicationContext.getBean(DataSource.class);
String resetSqlTemplate = "ALTER SEQUENCE %s RESTART WITH 1;";
try (Connection dbConnection = dataSource.getConnection()) {
for (String resetSqlArgument: tableNames) {
try (Statement statement = dbConnection.createStatement()) {
String resetSql = String.format(resetSqlTemplate, resetSqlArgument + "_id_seq");
statement.execute(resetSql);
}
}
}
}
}
有谁知道如何解决这个问题?
要看的一件事:
public static final Operation INSERT_USER =
insertInto("users")
.columns("id", "email", "password", "username")
.values(1, "krikkk998@mail.ru", "123123", "Daimon")
.build();
此处您使用的是硬编码 ID,即 1
现在,当您在测试用例中尝试创建另一个用户时,您将 id 作为 null 传递,假设它应该从序列中选择。它也会从 1
开始。因此,您会遇到冲突。
您遇到了与约束违规相关的问题。因此,您可以做的一件事是在 table 中将 'id'
列更改为 "auto_increment"
。数据库将负责自动增加此列值。
在任何时候,如果您想重置此 id 值,您可以调用 "resetAutoIncrementColumns()"
,然后在您的 INSERT SQL 中,您不必指定 'id'
专栏。保存新用户时,它总是会插入一个唯一值。
希望对您有所帮助。
我有这样的疑问。我正在使用 DBsetup 进行 spring 启动测试和 postgresql 数据库。我正在使用 DBsetup 设置用户,但是当我试图通过 spring 数据设置另一个用户时,我遇到了下一个异常:
Подробности: Key (id)=(1) already exists.
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [users_pkey];
这是我的测试class:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("/application-test.properties")
public class UserRepositoryTest {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private UserRepository userRepository;
@Autowired
private DataSource dataSource;
@Before
public void insertData() throws SQLException {
Operation operation = sequenceOf(CommonOperations.DELETE_ALL, CommonOperations.INSERT_USER);
DbSetup dbSetup = new DbSetup(new DataSourceDestination(dataSource), operation);
dbSetup.launch();
}
@After
public void cleanPK() throws SQLException {
DBUtil.resetAutoIncrementColumns(applicationContext, "user");
}
@Test
public void registerUser() {
val user = new User(null, "Glass", "123123", "glass999@mail.ru");
assertEquals(user, userRepository.saveAndFlush(user));
}
}
数据库设置操作:
public class CommonOperations {
public static final Operation DELETE_ALL = deleteAllFrom("article_tag", "article", "tag", "users");
public static final Operation INSERT_USER =
insertInto("users")
.columns("id", "email", "password", "username")
.values(1, "krikkk998@mail.ru", "123123", "Daimon")
.build();
}
Class 重置序列:
@NoArgsConstructor
public final class DBUtil {
public static void resetAutoIncrementColumns(ApplicationContext applicationContext,
String... tableNames) throws SQLException {
DataSource dataSource = applicationContext.getBean(DataSource.class);
String resetSqlTemplate = "ALTER SEQUENCE %s RESTART WITH 1;";
try (Connection dbConnection = dataSource.getConnection()) {
for (String resetSqlArgument: tableNames) {
try (Statement statement = dbConnection.createStatement()) {
String resetSql = String.format(resetSqlTemplate, resetSqlArgument + "_id_seq");
statement.execute(resetSql);
}
}
}
}
}
有谁知道如何解决这个问题?
要看的一件事:
public static final Operation INSERT_USER =
insertInto("users")
.columns("id", "email", "password", "username")
.values(1, "krikkk998@mail.ru", "123123", "Daimon")
.build();
此处您使用的是硬编码 ID,即 1
现在,当您在测试用例中尝试创建另一个用户时,您将 id 作为 null 传递,假设它应该从序列中选择。它也会从 1
开始。因此,您会遇到冲突。
您遇到了与约束违规相关的问题。因此,您可以做的一件事是在 table 中将 'id'
列更改为 "auto_increment"
。数据库将负责自动增加此列值。
在任何时候,如果您想重置此 id 值,您可以调用 "resetAutoIncrementColumns()"
,然后在您的 INSERT SQL 中,您不必指定 'id'
专栏。保存新用户时,它总是会插入一个唯一值。
希望对您有所帮助。