CyberMJ
CyberMJ

Reputation: 863

Odd behavior when using uppercase(combined with underscore) for column names in Hibernate(deployed on JBoss 5.1.0), for MySql DB

I'm trying to specify column name using the @Column annotation:

@Column(name="ID") //also tried with @Column(name="[ID]") and @Column(name=""\ID"\")
private int id; 

@Column(name="TINY_IMAGE")
private String tinyUrl; 

But, the hibernate (or MySQL?) gives the names 'id' and 'tinyUrl' for the above mentioned columns, respectively.

However, when I use the code bellow, columns' names in the join table are just fine (SHOE_ID and ARTICLE_ID):

@JoinTable(name="SHOE_ARTICLE",
        joinColumns={@JoinColumn(name="SHOE_ID")},
        inverseJoinColumns={@JoinColumn(name="ARTICLE_ID")})

Here is the content of persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

         version="2.0">
   <persistence-unit name="YSPers">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/ySDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" 
                  value="org.hibernate.dialect.MySQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.show_sql" value="false" />
         <property name="hibernate.format_sql" value="false"/>
         <!-- property name="hibernate.hbm2ddl.auto" value="create-drop"/-->
      </properties>
   </persistence-unit>
</persistence>       

How can I tell hibernate to use "name" property of @Column annotation for the column name?

Upvotes: 0

Views: 2058

Answers (1)

Emmanuel Bernard
Emmanuel Bernard

Reputation: 910

It looks like you might have mixed annotations on both fields and getters in the class hierarchy.

Make sure to put you annotations in on place. The target (field or getter) scanned by Hibernate depends on where you put the @Id annotation: make sure you put your @Column annotations on the same target. I usually recommend the getter for various reasons.

By the way, the way to quote a column name is by using backticks @Column(name="`MY_ID`")

Upvotes: 2

Related Questions