Hibernate @generatedvalue for HSQLDB

后端 未结 2 1048
悲&欢浪女
悲&欢浪女 2021-01-02 09:41

I have the following definition for an id field in an entity that is mapped to a table in HSQLDB.

...
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Colu         


        
2条回答
  •  死守一世寂寞
    2021-01-02 09:50

    I had the same issue when using a JpaSchemaGenerator utility class that I wrote.

    When generating the schema for a org.hibernate.dialect.HSQLDialect (where I use a SEQUENCE to generate my unique IDs), I use the following Hibernate property:

    hibernate.id.new_generator_mappings=true

    This results in the following CREATE statement:

    CREATE TABLE BATCH (
        BAT_ID NUMBER(19,0) NOT NULL,
        BAT_EXPIRY_DATE TIMESTAMP,
        BAT_NUMBER VARCHAR2(255 CHAR),
        BAT_MAT_ID NUMBER(19,0),
        PRIMARY KEY (BAT_ID)
    );
    

    But when I use this same property in my utility class to generate a schema using the org.hibernate.dialect.HSQLDialect, I get the following CREATE statement:

    CREATE TABLE BATCH (
        BAT_ID BIGINT NOT NULL,
        BAT_EXPIRY_DATE TIMESTAMP,
        BAT_NUMBER VARCHAR(255),
        BAT_MAT_ID BIGINT,
        PRIMARY KEY (BAT_ID)
    );
    

    This would mean that if I created a Batch without an ID, it would not generate it for me and the NOT NULL constraint would cause an exception.

    If I change the Hibernate property to the following:

    hibernate.id.new_generator_mappings=false

    Then it would generate the following CREATE statement:

    CREATE TABLE BATCH (
        BAT_ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
        BAT_EXPIRY_DATE TIMESTAMP,
        BAT_NUMBER VARCHAR(255),
        BAT_MAT_ID BIGINT,
        PRIMARY KEY (BAT_ID)
    );
    

    Which works perfectly when creating JPA entities with Hibernate.

提交回复
热议问题