deekay deekay - 1 month ago 16
Java Question

Hibernate expects sequence instead of table for postgres

I have my id generation defined as follows:

@Id
@GeneratedValue(generator = "pooled")
@GenericGenerator(name = "pooled", strategy = "org.hibernate.id.enhanced.TableGenerator", parameters = {
@Parameter(name = "value_column_name", value = "sequence_next_hi_value"),
@Parameter(name = "prefer_entity_table_as_segment_value", value = "true"),
@Parameter(name = "optimizer", value = "pooled-lo"),
@Parameter(name = "increment_size", value = "100")})
private Long id;


I'm using liquibase for table generation and let hibernate validate the schema, so that forgetting to edit the changelog will be detected. The liquibase definition for the
hibernate_sequences
looks like:

<changeSet author="david (generated)" id="1477051164467-23">
<createTable tableName="hibernate_sequences">
<column name="sequence_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="sequence_next_hi_value" type="bigint"/>
</createTable>
</changeSet>


This works for my local SQLServer Express as well as the h2 database that is used in unit tests. When I deploy the application on our dev server with postgres hibernate expects a database sequence instead of using the sequence table:
org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing sequence [hibernate_sequence]

I thought that using the TableGenerator strategy will not require sequences on any database.

Using Spring Boot with hibernate 5.0.11.Final

At this point we do not know what databases will be used in production so I need a portable solution. (at least supporting Oracle, MsSQL, Postgres, MySql... so in fact almost everything)

Update:
I have installed a local postgres and added the sequence by hand to check if it used. It is indeed used (the counter is 3) but I have no clue by which entity.
I have also hibernate envers enabled for entity auditing. The revinfo table uses a separate sequence, but this one is created by liquibase.

Turned out, the hibernate envers default entity uses
@GeneratedValue
hence the need for the sequence


Thanks in advance. If you need more information feel free to ask.

Answer

Please review your entities and check if all of them are using org.hibernate.id.enhanced.TableGenerator. That's the only possible cause - if you take a look at https://github.com/hibernate/hibernate-orm/blob/5.0.11/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java#L662 you will realize it doesn't require any sequences.

Comments