![](/img/trans.png)
[英]PostgreSQL Spring Boot - hibernate_sequence is not generated
[英]Spring boot can not find HIBERNATE_SEQUENCE
對於許多應用程序,我們需要的是將正確的 Spring Data 依賴項放在類路徑上。它工作正常:
配置:
@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages = {"io.boot.spring.entities"})
@EnableJpaRepositories(basePackages = {"io.boot.spring.repositories"})
@EnableTransactionManagement
public class ConfigForJPA {
@Bean
@ConfigurationProperties("spring.datasource.hikari")
public HikariDataSource dataSource() {
return (HikariDataSource) DataSourceBuilder.create()
.type(HikariDataSource.class).build();
}
}
應用程序屬性:
spring.datasource.hikari.jdbc-url=jdbc:h2:mem:mydb
spring.datasource.hikari.username=sa
spring.datasource.hikari.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.hibernate.use-new-id-generator-mappings=true
安慰:
Hibernate: drop table Blog if exists
Hibernate: drop table Item if exists
Hibernate: drop table Role if exists
Hibernate: drop table User if exists
Hibernate: drop table User_roles if exists
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table Blog
Hibernate: create table Item
Hibernate: create table Role
Hibernate: create table User
Hibernate: create table User_roles (users_id integer not null, roles_id integer not null)
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Hibernate: call next value for hibernate_sequence
Hibernate: insert into Role (name, id) values (?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into Role (name, id) values (?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into User (email, name, password, id) values (?, ?, ?, ?)
Hibernate: insert into User_roles (users_id, roles_id) values (?, ?)
Hibernate: insert into User_roles (users_id, roles_id) values (?, ?)
但是 Spring boot doc 說要完全控制 EntityManagerFactory 的配置,您需要添加一個名為“entityManagerFactory”的 @Bean,當我將它添加到我的配置時。
配置:
@Bean
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(dataSource())
.packages("io.boot.spring")
.persistenceUnit("io.boot.spring.entities")
.build();
}
它給出了錯誤:
安慰:
: HHH000412: Hibernate Core {5.0.11.Final}
: HHH000206: hibernate.properties not found
: HHH000021: Bytecode provider name : javassist
: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Started.
: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory
for persistence unit 'io.boot.spring.entities'
Hibernate: call next value for hibernate_sequence
: SQL Error: 90036, SQLState: 90036
: Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:
call next value for hibernate_sequence [90036-193]
為什么找不到 HIBERNATE_SEQUENCE? 我沒有修改任何東西只是將 entityManagerFactory bean 添加到配置文件
實體:
@Entity
public class Role {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Integer id;
private String name;
... getters and setters
解決辦法是:
CREATE TABLE CLIENT(
ID INTEGER NOT NULL,
CLIENT_NAME VARCHAR(255) NOT NULL,
ACTIVE CHAR(1) NOT NULL DEFAULT 'Y'
);
CREATE SEQUENCE CLIENT_SEQUENCE_ID START WITH (select max(ID) + 1 from CLIENT);
(這允許您使用靜態值預填充 CLIENT,並相應地初始化序列)
在 Java 中
@Id
@SequenceGenerator(name= "CLIENT_SEQUENCE", sequenceName = "CLIENT_SEQUENCE_ID", initialValue=1, allocationSize = 1)
@GeneratedValue(strategy=GenerationType.AUTO, generator="CLIENT_SEQUENCE")
private Long id
;
請按照上述說明操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.