gpt4 book ai didi

java - LiquiBase 和 Spring boot - 缺少序列

转载 作者:行者123 更新时间:2023-11-30 05:47:47 25 4
gpt4 key购买 nike

在 liquibase 中,我的变更集如下所示:

    <createSequence schemaName="public"
incrementBy="1"
minValue="1"
sequenceName="user_seq" />

<createTable tableName="user" schemaName="public">
<column name="id" type="bigint" defaultValueSequenceNext="user_seq">
<constraints nullable="false" primaryKey="true"/>
</column>
</createTable>

我的实体:

@Entity
@Table(name = "user")
public class User {
@SequenceGenerator(name="USER_SEQ",sequenceName="USER_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ")
@Id
protected long id;
}

Spring Boot 中的验证步骤未通过。 hibernate 抛出:

Schema-validation: missing sequence [public.user_seq]

LiquiBase 执行此查询:

CREATE SEQUENCE public.user_seq INCREMENT BY 1 MINVALUE 1
Sequence user_seq created

当我将 ddl-auto 更改为更新时,hibernate 执行此查询:创建序列public.user_seq开始1增量50并且 JDBC 抛出异常:序列“user_seq”已经存在; SQL语句:.

如何在LiquiBase中正确创建序列?

--@Edit1 - 我尝试在实体中使用小写: USER_SEQ -> user_seq - 没有帮助

最佳答案

我正在使用 Spring Boot

这是我的 application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:liquibase-changeLog.xml

这是我的实体

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ")
@SequenceGenerator(name = "USER_SEQ", sequenceName = "USER_SEQ", allocationSize = 1)
private long id;

private String name;

这是我的更改集

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

<changeSet author="EssexBoy" id="101">

<createSequence schemaName="public" startValue="1" incrementBy="1" ordered="true" sequenceName="user_seq"/>

<createTable tableName="user" schemaName="public">
<column name="id" type="bigint">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="name" type="varchar(50)"/>
</createTable>

</changeSet>

</databaseChangeLog>

这是我的测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class LiquibaseExampleApplicationTests {

@Autowired
private DataSource dataSource;

@Autowired
private UserRepository repository;

@Test
public void test1() throws Exception {
repository.save(makeUser("EssexBoy"));
repository.save(makeUser("EssexDad"));
repository.save(makeUser("EssexMum"));
repository.save(makeUser("EssexBaby"));

repository.findAll().forEach(System.out::println);
}

private User makeUser(String name) {
User user = new User();
user.setName(name);
return user;
}
}

输出为

User{id=1, name='EssexBoy'}
User{id=2, name='EssexDad'}
User{id=3, name='EssexMum'}
User{id=4, name='EssexBaby'}

关于java - LiquiBase 和 Spring boot - 缺少序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54550869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com