Git Product home page Git Product logo

Comments (3)

trynow-david avatar trynow-david commented on May 29, 2024

To note the further issue this causes is that when you name a column on a micronaut-data entity something like @column(name = "_my_column_name")
it will not select the right column in querying and causes issues, which you can't fix without changing the naming strategy

from micronaut-core.

trynow-david avatar trynow-david commented on May 29, 2024

For those who run into this issue, you can override the PhysicalNamingStrategy like this with the fix in it.

package com.trynow.micronaut

import io.micronaut.context.annotation.Primary
import io.micronaut.context.annotation.Replaces
import io.micronaut.data.hibernate.naming.DefaultPhysicalNamingStrategy
import io.micronaut.data.model.naming.NamingStrategy
import jakarta.inject.Singleton
import org.hibernate.boot.model.naming.Identifier
import org.hibernate.boot.model.naming.PhysicalNamingStrategy
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment

/***
 * This is an almost exact copy of the DefaultPhysicalNamingStrategy class with a fix for a bug
 * that occurs when an identifier start with an underscore.
 */
@Singleton
@Primary
@Replaces(DefaultPhysicalNamingStrategy::class)
class PhysicalNamingStrategyCustom : PhysicalNamingStrategy {
    override fun toPhysicalCatalogName(name: Identifier?, jdbcEnvironment: JdbcEnvironment?): Identifier? {
        return getIdentifier(name)
    }

    override fun toPhysicalSchemaName(name: Identifier?, jdbcEnvironment: JdbcEnvironment?): Identifier? {
        return getIdentifier(name)
    }

    override fun toPhysicalTableName(name: Identifier?, jdbcEnvironment: JdbcEnvironment?): Identifier? {
        return getIdentifier(name)
    }

    override fun toPhysicalSequenceName(name: Identifier?, jdbcEnvironment: JdbcEnvironment?): Identifier? {
        return getIdentifier(name)
    }

    override fun toPhysicalColumnName(name: Identifier?, jdbcEnvironment: JdbcEnvironment?): Identifier? {
        name?.let {
            // In this case the getIdentifier code causes double underscore to be prepended to a string,
            // so we just return the original name, and we will just handle that column name more explicitly
            if (name.text.startsWith("_")) {
                return name
            }
        }
        return getIdentifier(name)
    }

    private fun getIdentifier(name: Identifier?): Identifier? {
        return if (name == null) {
            null
        } else {
            Identifier(
                NamingStrategy.DEFAULT.mappedName(name.text),
                name.isQuoted
            )
        }
    }
}

from micronaut-core.

wetted avatar wetted commented on May 29, 2024

underscoreSeparate("_nameOfThing") should result in "_name_of_thing" but actually results in "__name_of_thing"

Actually, the expected result should be _name_Of_Thing because the NameUtils.underscoreSeparate does not perform lowercase. I will overload the call to support it with underscoreSeparate(String camelCase, boolean lowercase)

from micronaut-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.