Incompatible sequence names between Liquibase and Hibernate JPA for PostgreSQL and H2

I have an application working with the standard Hibernate JPA and Liquibase to generate db. I use H2 for testing and PostgreSQL at startup.

My problem is that I cannot get this setting to work well for primary keys with sequence generation.

When I have an object identifier, for example:

@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; 

and use Liquibase to create the database, for example:

 <column name="id" type="BIGINT" autoIncrement="true" incrementBy="1"> <constraints nullable="false" primaryKey="true" /> </column> 

it works fine in H2, but for PostgreSQL Hibernate complains that it is:

 "Missing sequence or table: hibernate_sequence" 

I can fix this for PostgreSQL by changing the JPA @GeneratedValue to something like:

 @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "text_id_seq") @SequenceGenerator(name = "text_id_seq", sequenceName = "text_id_seq", allocationSize = 1) private Long id; 

But now the H2 sequence will not match the expected Hibernate.

There seems to be no easy way to make sure that Liquibase is a sequence with a specific name. What can I do to make this setting work?

It seems to me that I am currently working

  • Liquibase.version 2.0.4
  • hibernate 4.1.7
  • postgres driver 9.1-901.jdbc3
  • postgres 9.2.1 (at least locally)
  • h2 1.3.168
+4
source share
1 answer

Use GenerationType.IDENTITY . This should work on most databases.

It seems that identity generation is one of JPA's warts; it is too difficult to set global overrides for generation options based on the database you are using, etc. Doing this in the annotation makes programming difficult.

Sleeping bizarre hibernate_sequence especially painful. This is outside of me, why it doesn't use the default PostgreSQL sequences for generated columns.

+4
source

Source: https://habr.com/ru/post/1443162/


All Articles