[英]LiquiBase & Spring boot - missing sequence
在 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 中的验证步骤未通过。 休眠抛出:
模式验证:缺少序列 [public.user_seq]
LiquiBase 执行此查询:
CREATE SEQUENCE public.user_seq INCREMENT BY 1 MINVALUE 1
Sequence user_seq created
当我将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:
.
如何在 LiquiBase 中正确创建序列?
-- @Edit1 - 我尝试在实体中使用小写字母:USER_SEQ -> user_seq - 没有帮助
我正在使用弹簧靴
这是我的 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'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.