Category: hibernate

How to escape reserved words used as column names – Hibernate


We may come up with a situation where MYSQL reserved key word as column name, say ‘from’ , ‘to’  as column names. But hibernate will NOT escape these reserve words by default while doing CRUD operation on the table. Which leads to the below exception ” org.hibernate.exception.SQLGrammarException”.

To over come this issue we need to add the below property in hibernate configuration file.

<prop key="hibernate.globally_quoted_identifiers">true</prop>


<property name="hibernate.globally_quoted_identifiers">true</property>


Alternate solution:

1.Enclose the keyword with square brackets []

<property name="desc" type="string" >
 <column name="[DESC]" length="255" not-null="true" />

2. Use single quote(‘) to enclose the double quotes (“)

<property name="desc" type="string" >
<column name='"DESC"' length="255" not-null="true" />

Generic way of mapping hbm file

We no need to map individual hbm files simply add the below highlighted property which solves the problem

<bean id=”factory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean” destroy-method=”destroy”>
<property name=”mappingLocations” value=”classpath:**/*.hbm.xml” />
<property name=”hibernateProperties”>
<prop key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>
<prop key=”connection.provider_class”>org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key=”hibernate.c3p0.max_size”>5</prop>
<prop key=”hibernate.c3p0.min_size”>1</prop>
<prop key=”hibernate.c3p0.idle_test_period”>60</prop>
<prop key=”hibernate.c3p0.timeout”>10</prop>
<prop key=”hibernate.connection.autoReconnect”>true</prop>

<property name=”dataSource”>
<ref bean=”dataSource” />