I am trying to create a data table and map this table to an entity using a H2 in-memory database. I am using spring-boot (release 2.0.2) and flyway to create the database before running.
For some reason I am not successful to map the identity column when hibernate is to validate against the schema.
Suggestions?
Table:
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:
@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;
...
}
Mapped superclass:
@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:
<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
I have tried every GenerationType
(strategy in @GeneratedValue
)
When the spring-boot application is started, the application context fails when hibernate will validate the schema:
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]
I move forward without resolving this problem. I have created an aspect driven solution for the problem which will enter a sequenced number for an id if none is provided.
I know this is a brittled solution, but I can move forward and focus on more dire needs. This remain a problem that should be solved. I updated the repository on git-hub with this solution: https://github.com/jactor-rises/jactor