You were right! It did the trick! Thanks a lot.
For anyone who might be interested, here's what I did:
1. create a custom naming strategy class:
Code:
package com.mdpframework.dataaccess.hibernate;
import org.hibernate.cfg.DefaultNamingStrategy;
import org.hibernate.dialect.Dialect;
public class DefaultQuotedNamingStrategy extends DefaultNamingStrategy
{
private static Dialect _dialect = new MySQLInnoDBUTF8Dialect();
@Override
public String classToTableName(String className)
{
System.out.println("Adding quotes to table from class "+className);
return addQuotes(super.classToTableName(className));
}
@Override
public String tableName(String tableName)
{
System.out.println("Adding quotes to table "+tableName);
return addQuotes(super.tableName(tableName));
}
/**
* Adds opening and closing quotes as provided by the current dialect.
*
* @param input
* the input to quote
* @return the qouted input
*/
private static String addQuotes(String input)
{
return new StringBuffer().append(_dialect.openQuote())
.append(input)
.append(_dialect.closeQuote()).toString();
}
}
Unfortunately, what I didn't get to work is, load the dialect from the settings. When using
Code:
private static Dialect _dialect = Dialect.getDialect();
I got an exception, telling me there is no dialect set. It's some kind of config issue, as the config says
Code:
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
...
<prop key="hibernate.dialect">com.mdpframework.dataaccess.hibernate.MySQLInnoDBUTF8Dialect</prop>
...
</props>
</property>
</bean>
Usually that works. Aaaanyway
2. Set the naming strategy
Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" autowire-candidate="true" depends-on="hibernateDataSource">
<property name="dataSource" ref="hibernateDataSource" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="namingStrategy" ref="namingStrategy"/>
<property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property>
<property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property>
</bean>
<bean id="namingStrategy" class="com.movilitas.mdpframework.dataaccess.hibernate.DefaultQuotedNamingStrategy"/>
3. Stop worrying about table names that are SQL reserved words :)
One could extend the above class to also quote column names to use special words for column names as well.
Thanks again!
All the best,
Vassil