Hibernate, force table name in low box

Asked

Viewed 374 times

3

I am using Hibernate 5.2.1.Final as an implementation to handle Mysql database. The problem is that the tables are being generated with the first letter in high box, would like to force to be all in low box via persistence file. Currently my file is this:

<persistence-unit name="testeUP" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.56.100:3306/locadora"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="1QAZxsw2"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.diaLect" value="org.hibernate.diaLect.MySQLDiaLect"/>
    </properties>
</persistence-unit>

Is it possible to force this behavior? If so, how?

  • Is there a reason to use uppercase L instead of lowercase in words dialect of "hibernate.diaLect" and also in "org.hibernate.diaLect.MySQLDiaLect"? I think this should be tiny.

  • Friend I suggest you consult this material, unfortunately I am without time to help you but I suggest reading this tutorial (it is in English): [Implementing a Custom Naming Strategy With Hibernate](https://www.petrikainulainen.net/programming/tips-and-tricks/implementing-a-custom-namingstrategy-with-Hibernate/ "Implementing a Custom Naming Strategy With Hibernate")

1 answer

5

In your entities, use the annotation @Table. Here’s an example:

@Entity
@Table(name = "tabela_caixa_baixa")
public class MinhaTabela implements Serializable {
    // ...
}

When the annotation @Table is absent, it is understood that the table name will be the default (default). The default is to leave the persistence Provider (in his case, Hibernate) give the name he thinks best.

Since what you want is a different behavior from the default (don’t let Hibernate give the name the way he wants), then you specify the desired behavior by using the annotation.

  • I get it, it helps a lot. But there is some configuration that at least makes Hibernate not take into account the difference of high and low box as to the table names?

  • @Fábio What do you mean? Databases in general do not differentiate between upper and lower case for table and column names in type instructions SELECT, INSERT, UPDATE and DELETE. So if your note is "tabela_caixa_baixa", but in the database "Tabela_CAIXA_BaIXa", will work the same way the application. The only significant difference is that if/when Hibernate gives a CREATE TABLE, it will send the table name to the database the way it is in the annotation and so it will be registered.

  • Currently I am not using the tag to indicate the name of the tables, IE, Hibernate is taking the name of the class. In the persistence file I am using an instruction to create the table if it does not exist in the bank, however, I already have the table with the name written in low box, but Hibernate is creating a variation in high box according to the standard transcribed in the class. I wanted to ignore the way you’re in the bank and consider whether it’s low or high.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.