Jakkins
Jakkins

Reputation: 175

How to setup hibernate.id.db_structure_naming_strategy to legacy in springboot?

Due to dependencies upgrade:

I started to use hibernate-core-6.1.5 with his new strategy to give names to tables. So now tests are broken because when I want to save data to some some table a "_SEQ" is added to the table's name and the test won't find the table.

Investigating I finded some source:

To bypass the problem I'm specifying the strategy inside every entity:

@Entity
@Table(name = "animal")
public class Animal {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)  // this will overwrite SEQUENCE
    @Column(name = "id")
    private Long id;

Could this be done in the application.yml?

Upvotes: 3

Views: 6602

Answers (1)

Jakkins
Jakkins

Reputation: 175

Thanks to M. Deinum the problem is solved.

Now my application.yml is:

spring.jpa.properties.hibernate.id.db_structure_naming_strategy: legacy
spring.jpa.hibernate.naming:
  implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
  physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring:
  profiles.active: dev
  jpa.hibernate.use-new-id-generator-mappings: false
  jpa.properties.jakarta.persistence.sharedCache.mode: ALL
  hibernate.show_sql: true
  flyway.out-of-order: false

# other stuff

update

As of today I have new informations:

Hibernate changes its GeneratedType.AUTO strategy since 5.2

this is another solution

application.yml

spring:
  profiles.active: dev
  datasource:
    url: jdbc:sqlite:/data.db
    driver-class-name: org.sqlite.JDBC
  jpa:
    database-platform: org.hibernate.community.dialect.SQLiteDialect

---

spring:
  config:
    activate:
      on-profile: dev
  main:
    banner-mode: off
  # jpa.show-sql: true

logging.level:
  root: INFO
  level: DEBUG
  org.springframework.web: DEBUG
  org.hibernate.type: DEBUG
  # org.hibernate.SQL: DEBUG

The Entity:

@Entity
@Table(name = "files")
@Data
class FileEntity() {

    constructor(originalFilename: String, calculateHash: String) : this() {
        this.filename = originalFilename
        this.hash = calculateHash
    }

    @Id
    @GenericGenerator(name = "gen", strategy = "increment")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "gen")
    @Column(name = "id", unique = true, nullable = false)
    private var id: Long? = null

    @Column(nullable = false)
    private var filename: String? = null

    @Column(nullable = false, unique = true)
    private var hash: String? = null
}

source : https://stackoverflow.com/a/11390603/16988820

Upvotes: 4

Related Questions