1

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

jactor-rises
  • 2,395
  • 2
  • 22
  • 44

1 Answers1

0

Please go through this and search GenerationType.SEQUENCE.

Deductions based on the read :

@Id @GeneratedValue(generator = "address_seq", strategy = GenerationType. SEQUENCE) 
@SequenceGenerator(name="<name of schema in H2>", sequenceName = "address_seq", allocationSize=1)
private Long id;
Ankush
  • 499
  • 1
  • 7
  • 17
  • I did go through this documentation, but it is still not working. Maybe because I am not creating a database SCHEMA? I am using "PUBLIC" as this is the default when no schema is provided... – jactor-rises May 21 '18 at 15:41
  • As per https://stackoverflow.com/questions/12745751/hibernate-sequencegenerator-and-allocationsize also add `` in hibernate properties. – Ankush May 21 '18 at 17:03
  • I am using spring-jpa and do not have a hibernate.properties. the only property file is application.properties (look above) – jactor-rises May 21 '18 at 17:37
  • Here is the equivalent in spring-jpa: `spring.jpa.hibernate.use-new-id-generator-mappings=true` from https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html – Ankush May 22 '18 at 05:33
  • sorry... no sigar. this is the cause of the failure now: Caused by: org.hibernate.AnnotationException: Unknown Id.generator: address_seq – jactor-rises May 22 '18 at 11:03
  • I tried to rewrite without using validation, but now I cannot get it to work???? I need to resolve this strange issue... – jactor-rises May 22 '18 at 11:25
  • I am stuck with this problem... If someone is able to see what I am doing wrong, I would be grateful. Github: https://github.com/jactor-rises/jactor – jactor-rises May 24 '18 at 14:35
  • @jactor-rises: how can I run the client application, it says no manifest available and I could not find any file to start the client. – Ankush Jul 13 '18 at 07:27
  • @Akush - first build the application (jactor/build.sh), then goto folder jactor/jactor-standalone/persistence-orm and type mvn spring-boot:run. The application is startet on localhost (port 8080) without any context root – jactor-rises Jul 14 '18 at 11:00
  • there are no client. I would like to add sequences using the h2 database. AddressRepositoryTest should work unchanged when adding sequences in the database and removing the IdentitySequencer located in the aop package... – jactor-rises Jul 14 '18 at 11:06
  • the full project is now "released" with this error-prone solution. the jactor repository is replaced by full code: https://github.com/jactor-rises/jactor-rises – jactor-rises Jul 25 '18 at 11:43