Customize the primary key name with Liquibase and Hibernate for liquibase:diff

159 Views Asked by At

I have a Spring Boot application where I am trying to automatically generate a liquibase diff .sql (using mvn liquibase:diff) with my own NamingStrategy. I created a custom ImplicitNamingStrategy and PhysicalNamingStrategy and provided both for Hibernate and Liquibase. The goal is to have all fields in lowercase, as I don't want liquibase to add quotes to it (as postgres should not create these tables/columns as case sensitive)

It works for all fields except the primary key field, that is always created as "<tablename>PK". I try to have it generated as "<tablename>pk" (lowercase).

I am using Spring Boot 3.1.3, Postgresql 15.3, Liquibase 4.22.0 and Hibernate 5.4.17.Final.

Example Entity:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DbTest {

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

    String text;

    String moreText;
}

Liquibase.properties:

changeLogFile=db/liquibase/db.changelog-master.yaml

# Hibernate connection, used by mvn liquibase:diff
referenceUrl=hibernate:spring:licenseserver.entities\
  ?dialect=org.hibernate.dialect.PostgreSQLDialect\
  &hibernate.physical_naming_strategy=test.MyPhysicalNamingStrategy\
  &hibernate.implicit_naming_strategy=test.MyImplicitNamingStrategy

Generated Diff:

-- liquibase formatted sql

-- changeset fab:6648-1
CREATE TABLE dbtest (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, moretext VARCHAR(255), text VARCHAR(255), CONSTRAINT "dbtestPK" PRIMARY KEY (id));

I do not understand where the constraint name "dbtestPK" is generated. As far as I tried, Hibernate creates it with "PK_" (without liquibase by using hibernate.hbm2ddl.auto=update) , liquibase defaults to "_pkey" (based on my understanding of liquibase.database.corePostgresDatabase.java).

Where is it generated? How could I customize it?

I do not believe that https://hibernate.atlassian.net/browse/HHH-11586 applies here, as my approach above works for unique constraints as well, just not for the primary key.

Edit: I also tried setting it explicitly using @UniqueConstraint - it does not work for the primary key.

0

There are 0 best solutions below