Java Spring: AnnotationSessionFactoryBean, Hibernate-Dialect AutoDetection
I am using the following spring application context:
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<!--
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
-->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.jdbc.batch_size">1000</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
...
</list>
</property>
<property name="schemaUpdate" value="${hibernate.schemaUpdate}"/>
</bean>
Then the problem is this: although Hibernate has to support Dialect-Autodetection, this code doesn't work: Uppon Application-Init throws the following exception:
org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
at org.hibernate.dialect.Dialect.instantiateDialect(Dialect.java:256)
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:234)
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:249)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean$3.doInHibernate(LocalSessionFactoryBean.java:957)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.updateDatabaseSchema(LocalSessionFactoryBean.java:953)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterSessionFactoryCreation(LocalSessionFactoryBean.java:843)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:213)
I can avoid this exception by uncommenting the hibernate.dialect property. Why should I do this? Why can't hibernate just auto-detect the dialect as it is supposed to?
I want to remove the dialect property to solve this problem .
source to share
Hibernate attempts to automatically detect the dialect during a call Configuration.buildSessioNFactory()
- and usually, though not always, succeeds.
Spring code, however, is not - and that means you are getting an exception. LocalSessionFactoryBean.updateDatabaseSchema()
the method uses a method Dialect.getDialect()
to get an instance Dialect
that requires an explicitly specified dialect.
I don't quite understand why this is the case, although I suspect that this precaution against Hibernate mistakenly detects your dialect incorrectly and messes up your database as a result (since the update scheme could potentially be quite catastrophic).
Your options:
- Explicitly specifying a dialect
- Setting
schemaUpdate
to false - Extending LocalSessionFactoryBean and overriding a method
updateDatabaseSchema()
to accept the (already auto-detected) dialect from the factory session.
You need the class to pass it to the SessionFactoryImplementor for this:
public void updateDatabaseSchema() throws DataAccessException {
final Dialect dialect = ((SessionFactoryImplementor) getSessionFactory()).getDialect();
...
hibernateTemplate.execute(new HibernateCallback() {
...
// you already have the dialect so you don't need to get it using this:
// Dialect dialect = Dialect.getDialect(getConfiguration().getProperties());
...
);
}
source to share