1

I'm new to Micronaut and I'm trying to use it with an existing database.

I have an entity with explicit table and column naming, like

@Entity
@Table(name = "TGE040LABEL", schema = "dbo", catalog = "tp_63_dev")
@IdClass(Tge040LabelEntityPK.class)
public class Tge040LabelEntity

and even if I configure physical_naming_strategy in application.yml like this (and variants, I've tried various values for physical_naming_strategy... ):

jpa:
    default:
        entity-scan:
            packages:
                - 'my.app.domain'
        properties:
            hibernate:
                hbm2ddl:
                    auto: none
                show_sql: true
                physical_naming_strategy: "io.micronaut.data.model.naming.NamingStrategies.UpperCase"
                dialect: "org.hibernate.dialect.SQLServerDialect"

I still get this error

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'tp_63_dev.dbo.tge040_label'.

as if my configuration completely gets ignored.

Micronaut version is micronautVersion=1.3.3

Am I missing something?

UPDATE

Before:

jpa:
    default:
        entity-scan:
            packages:
                - 'my.app.domain'
        properties:
            hibernate:
                hbm2ddl:
                    auto: none
                show_sql: true
                dialect: "org.hibernate.dialect.SQLServerDialect"

enter image description here

After:

jpa:
    default:
        entity-scan:
            packages:
                - 'my.app.domain'
        properties:
            hibernate:
                hbm2ddl:
                    auto: none
                show_sql: false
                dialect: "org.hibernate.dialect.SQLServerDialect"
                physical_naming_strategy: 'org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl'

enter image description here

Just to say, config is in the right place, changes to show_sql are honored, changes to physical_naming_strategy are apparently ignored.

Is it actually possible to change the physical_naming_strategy?

UPDATE 2

Changed my configuration like this:

jpa:
    default:
        entity-scan:
            packages:
                - 'my.app.domain'
        properties:
            hibernate:
                id:
                    new_generator_mappings: false
                format_sql: true
                globally_quoted_identifiers_skip_column_definitions: true
                jdbc:
                    lob:
                        non_contextual_creation: true
                dialect: org.hibernate.dialect.SQLServerDialect
                ddl-auto: none
                physical_naming_strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
                implicit_naming_strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

and debugging I get this:

enter image description here

meaning in my opinion that micronaut is actively overriding my configuration, which is at least non-intuitive. Is this behavior documented somewhere?

2 Answers2

3

I finally found it.

The documentation for DefaultPhysicalNamingStrategy states:

The default PhysicalNamingStrategy to use. Can be replaced with another bean that declares: @Replaces(DefaultPhysicalNamingStrategy.class) and implements PhysicalNamingStrategy

This apparently means that there is actually no way to change the physical naming strategy from application.yml.

In fact, implementing a custom naming strategy bean like this:

@Replaces(DefaultPhysicalNamingStrategy.class)
public class PhysicalNamingStrategyCustom implements PhysicalNamingStrategy
{

    @Override
    public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment)
    {
        return name;
    }

    @Override
    public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment)
    {
        return name;
    }

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment)
    {
        return name;
    }

    @Override
    public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment)
    {
        return name;
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment)
    {
        return name;
    }
}

did the trick, and JPA/hibernate is no longer trying to be smart about my table and field names.

  • I had to use `@Singleton` and `@Primary` to make this work in Micronaut 3.5. Anyway, thanks a lot. BR – Roar S. Aug 04 '22 at 18:18
0

We have the same issue using PostgreSQL. We fixed it by using

Pojo :

@Table(name="\"TBLTABLE\"", schema="a_schema"

application.yml :

jpa:
database: POSTGRESQL
show-sql: true
open-in-view: false
generate-ddl: false
properties:
  hibernate:
    id:
      new_generator_mappings: false
    format_sql: true
    globally_quoted_identifiers_skip_column_definitions: true
    jdbc:
      lob:
        non_contextual_creation: true
    dialect: org.hibernate.dialect.PostgreSQL9Dialect
hibernate:
  ddl-auto: none
  naming:
    physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
database-platform: org.hibernate.dialect.PostgreSQLDialect
3logy
  • 2,634
  • 8
  • 46
  • 99