简体   繁体   中英

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?

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`")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM