Home > Net >  Configure diff table name in hibernate for MySQL and SQL server for the same entity
Configure diff table name in hibernate for MySQL and SQL server for the same entity

Time:11-27

I have an entity with table name "Transaction". While creating connection using spring orm and hibernate, on startup it successfully creates tables in MySQL but being "Transaction" a keyword in SQL server, it fails on startup. I cannot change the table name in the code as there is a lot of ripple effects. Hence is there a way where I can define the table name as Transaction in java and intercept or configure in such a way that hibernate while connecting to SQL server escapes the keyword and queries it as "[Transaction]" (escaping it using [])

CodePudding user response:

You can name the table via @Table(name="your_table_name")

CodePudding user response:

You can use a naming strategy as explained in this article: https://www.baeldung.com/hibernate-naming-strategy

public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
    @Override
    public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
        return Identifier.toIdentifier(**your table name**);
    }

    // Other methods
  • Related