我正在尝试创建一个数据表,并使用H2内存数据库将此表映射到实体。我在运行之前使用spring-boot(版本2.0.2)和flyway来创建数据库。
出于某种原因,当hibernate要对模式进行验证时,我没有成功映射标识列。
建议?
表:
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>
application.properties:
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
答案 0 :(得分:0)
请浏览this并搜索GenerationType.SEQUENCE。
基于阅读的扣减:
@Id @GeneratedValue(generator = "address_seq", strategy = GenerationType. SEQUENCE)
@SequenceGenerator(name="<name of schema in H2>", sequenceName = "address_seq", allocationSize=1)
private Long id;