我正在尝试创建一个数据表并将该表映射到使用 H2 内存数据库的实体。我正在使用 spring-boot(2.0.2 版)和 flyway 在运行之前创建数据库。
由于某种原因,当休眠要针对架构进行验证时,我无法成功映射标识列。
建议?
桌子:
CREATE TABLE T_ADDRESS (
ID BIGINT(19) PRIMARY KEY,
CREATION_TIME TIMESTAMP NOT NULL,
CREATED_BY VARCHAR(50) NOT NULL,
UPDATED_TIME TIMESTAMP NOT NULL,
UPDATED_BY VARCHAR(50) NOT NULL,
ADDRESS_LINE_1 VARCHAR(50) NOT NULL,
ADDRESS_LINE_2 VARCHAR(50),
ADDRESS_LINE_3 VARCHAR(50),
CITY VARCHAR(50) NOT NULL,
COUNTRY VARCHAR(7),
ZIP_CODE INTEGER(4) NOT NULL
);
实体:
@Entity
@Table(name = "T_ADDRESS")
public class AddressEntity extends PersistentEntity {
@Id private Long id;
@Column(name = "ADDRESS_LINE_1", nullable = false) private String addressLine1;
@Column(name = "ADDRESS_LINE_2") private String addressLine2;
@Column(name = "ADDRESS_LINE_3") private String addressLine3;
@Column(name = "CITY", nullable = false) private String city;
@Column(name = "COUNTRY") private String country;
@Column(name = "ZIP_CODE", nullable = false) private Integer zipCode;
...
}
映射超类:
@MappedSuperclass
public abstract class PersistentEntity {
@Column(name = "CREATION_TIME") private LocalDateTime creationTime;
@Column(name = "CREATED_BY") private String createdBy;
@Column(name = "UPDATED_TIME") private LocalDateTime updatedTime;
@Column(name = "UPDATED_BY") private String updatedBy;
...
}
Maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
应用程序属性:
spring.datasource.url=jdbc:h2:mem:jactor
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=validate
我已经尝试了每一个GenerationType
(策略@GeneratedValue
)
当 spring-boot 应用程序启动时,应用程序上下文在 hibernate 将验证模式时失败:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at ...
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: address_seq
at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:665) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at ...
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
我在没有解决这个问题的情况下继续前进。我已经为这个问题创建了一个方面驱动的解决方案,如果没有提供,它将输入一个 id 的序列号。
我知道这是一个脆弱的解决方案,但我可以继续前进并专注于更迫切的需求。这仍然是一个应该解决的问题。我用这个解决方案更新了 git-hub 上的存储库:https ://github.com/jactor-rises/jactor