Repository navigation
Closed
Labels
Milestone
Description
James Roper opened SPR-7936 and commented
Hibernate is able to automatically detect the dialect if none is configured. However, if none is configured on LocalSessionFactoryBean, and schemaUpdate is set to true, then the following exception is thrown:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.orm.hibernate3.HibernateSystemException: The dialect was not set. Set the property hibernate.dialect.; nested exception is org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:435)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:409)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:537)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:289)
... 40 more
Caused by: org.springframework.orm.hibernate3.HibernateSystemException: The dialect was not set. Set the property hibernate.dialect.; nested exception is org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
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)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 55 more
Caused by: org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
at org.hibernate.dialect.Dialect.instantiateDialect(Dialect.java:305)
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:283)
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:298)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean$3.doInHibernate(LocalSessionFactoryBean.java:957)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 61 more
The following implementation of updateDatabaseSchema() fixes the problem:
public void updateDatabaseSchema() throws DataAccessException
{
logger.info("Updating database schema for Hibernate SessionFactory");
SessionFactory sessionFactory = getSessionFactory();
final Dialect dialect = ((SessionFactoryImplementor)sessionFactory).getDialect();
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
Connection con = session.connection();
DatabaseMetadata metadata = new DatabaseMetadata(con, dialect);
String[] sql = getConfiguration().generateSchemaUpdateScript(dialect, metadata);
executeSchemaScript(con, sql);
return null;
}
}
);
}Affects: 3.0.5
Issue Links:
- Introduce (Annotation)SessionFactoryBuilder types [SPR-8066] #12721 Introduce (Annotation)SessionFactoryBuilder types ("depends on")
Activity
spring-projects-issues commentedon Mar 19, 2011
Chris Beams commented
Thanks, James! Your suggestion above works well. I'm resolving this issue now, but note that the code is not yet merged into trunk (it's on this branch at github: https://github.com/cbeams/spring-framework/tree/SPR-8066). It will of course be merged by the time we release 3.1 M2, and probably much sooner. You can watch the 'Builds' tab below to see when a snapshot build exists that includes the fix (actually, our Jira<->Bamboo integration is down at the moment, but I've just requested it be fixed for cases just such as this one). You can still subscribe to the RSS feed, though, and when it's back up you should be notified.
spring-projects-issues commentedon Mar 9, 2012
André Schäfer commented
This fix is not merged into any release, yet.
spring-projects-issues commentedon Mar 9, 2012
Chris Beams commented
Good catch, André! This change actually was implemented in the 3.1 timeline, but inadvertently backed out in commit 3bb01ee. It's back in now.