Spring Boot 2+HikariCP报错:未指定对象创建方案,实体类自动创建失效
Hey there, let's work through these problems one by one—Spring Boot 2.0 threw in some breaking changes that trip up a lot of folks coming from 1.5.x. I've debugged similar upgrade headaches before, so here's what you need to do:
1. Fix Entity Auto-Creation & Schema.sql Execution
The biggest gotchas here are default behavior changes in Spring Boot 2.0:
- Datasource initialization mode: By default, Spring Boot 2.0 only runs
schema.sql/data.sqlfor in-memory databases (like H2). For PostgreSQL, you need to explicitly enable it. - JPA auto-ddl setting: For non-embedded databases,
spring.jpa.hibernate.ddl-autonow defaults tonone(meaning Hibernate won't create tables from your@Entityclasses). - Hikari configuration structure: Hikari properties moved under a
hikarinested node (no longer directly underdatasource). - JDBC URL property:
jdbcUrlfrom 1.5.x was renamed tojdbc-url.
Update your application.properties like this:
spring: datasource: driver-class-name: org.postgresql.Driver jdbc-url: jdbc:postgresql://localhost:5432/database # Fixed property name, added host/port for clarity username: root password: root schema: classpath:/db/init/schema.sql type: com.zaxxer.hikari.HikariDataSource initialization-mode: always # Enable schema.sql execution for PostgreSQL # Hikari properties now nested under hikari hikari: pool-name: SpringBootHikariCP maximum-pool-size: 5 jpa: hibernate: ddl-auto: update # Tell Hibernate to auto-create/update tables from entities show-sql: true # Optional, helps debug table creation properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect # Ensure proper PostgreSQL dialect
2. Fix PostgreSQL Array Support
If you're trying to create array columns via schema.sql, make sure your SQL uses valid PostgreSQL syntax:
-- Example schema.sql entry for a text array CREATE TABLE your_table ( id SERIAL PRIMARY KEY, your_array_column TEXT[] );
If you're mapping array fields in your @Entity classes, you'll need extra Hibernate type support. Spring Boot 2.0 uses Hibernate 5.2, so add this dependency (Maven example):
<dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.10.0</version> </dependency>
Then annotate your entity's array field correctly:
import com.vladmihalcea.hibernate.type.array.StringArrayType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @Entity @TypeDef(name = "string-array", typeClass = StringArrayType.class) public class YourEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Type(type = "string-array") @Column(columnDefinition = "text[]") private String[] yourArray; // Getters and setters }
3. Resolve "ERROR: the object creation scheme is not indicated"
This error is almost always a side effect of the configuration issues above:
- Either Hibernate didn't know how to handle entity table creation (because
ddl-autowas set tononeby default) - Or your
schema.sqlwasn't executing (becauseinitialization-modewas stuck onembedded)
Once you apply the configuration fixes from step 1, this error should disappear. If it persists, double-check your schema.sql for invalid PostgreSQL syntax or duplicate object names.
内容的提问来源于stack exchange,提问作者JONKI

