LiquiBase和Spring Boot - 序列丢失

4
在liquibase中,我的changeset如下所示:
    <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更改为update时,hibernate执行以下查询:create sequence public.user_seq start 1 increment 50 然后JDBC抛出异常:Sequence "user_seq" already exists; SQL statement:

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

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


请查看此答案,并确认您的更改已成功。 - Essex Boy
jdbc:h2:mem:testdb I have whole db in memory so create table starts every time when spring boot starts, changeset is correctly executed. Look at my question(higher) I wrote that LiquiBase execute this SQL: CREATE SEQUENCE public.user_seq INCREMENT BY 1 MINVALUE 1 - dramcio
1个回答

2
我将翻译以下内容:

我正在使用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'}

如果您将“ddl-auto”从“none”更改为“validate”,它是否仍然有效? - dramcio
@dramcio 是的,因为验证只是一个检查,不会发生数据库创建。 - Essex Boy
我有类似的代码,但当我改为验证时,Spring 就无法启动。 - dramcio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接