简体   繁体   English

LiquiBase & Spring boot - 缺少序列

[英]LiquiBase & Spring boot - missing sequence

In liquibase my changeset looks as below:在 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>

My entity:我的实体:

@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;
}

Validation step in spring boot does not pass. Spring Boot 中的验证步骤未通过。 Hibernate throws:休眠抛出:

Schema-validation: missing sequence [public.user_seq]模式验证:缺少序列 [public.user_seq]

LiquiBase execute this query: LiquiBase 执行此查询:

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

When I change ddl-auto to update, hibernate execute this query: create sequence public.user_seq start 1 increment 50 And JDBC throws exception: Sequence "user_seq" already exists; SQL statement:当我将ddl-auto更改为更新时,hibernate 执行此查询: create sequence public.user_seq start 1 increment 50 And JDBC throws exception: Sequence "user_seq" already exists; SQL statement: Sequence "user_seq" already exists; SQL statement: . Sequence "user_seq" already exists; SQL statement: .

How to correctly create sequence in LiquiBase?如何在 LiquiBase 中正确创建序列?

-- @Edit1 - I Try to use lowercase in entity: USER_SEQ -> user_seq - does not help -- @Edit1 - 我尝试在实体中使用小写字母:USER_SEQ -> user_seq - 没有帮助

I'm using spring boot我正在使用弹簧靴

Here's my application.properties这是我的 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

Here's my entity这是我的实体

@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;

Here's my change set这是我的更改集

<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>

Here's my test这是我的测试

@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;
    }
}

Output is输出是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM