Create bean hibernateDatastore so the project can be upgraded to Grails 2.1.1+
Grails 2.1.1 introduces a hibernateDatastore Spring bean (JIRA: http://jira.grails.org/browse/GRAILS-9373 - changeset: https://github.com/grails/grails-core/commit/a5028db9628e99d60ab0102bae980bbdfb5dc241).
I tried creating the bean in the Spring xml config file (like the other Grails beans) like this:
<bean id="hibernateDatastore" class="org.codehaus.groovy.grails.orm.hibernate.HibernateDatastore">
<constructor-arg ref="grailsDomainClassMappingContext"/>
<constructor-arg ref="sessionFactory"/>
<constructor-arg value="#{grailsApplication.config}"/>
</bean>
However, it fails with an exception: "org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'hibernateDatastore': Requested bean is currently in creation: Is there an unresolvable circular reference?".
The problem seems to be caused by passing the sessionFactory bean.
I'm still working on getting this working, but I'm on my way.
I had to rearrange applicationContext.xml so that transactionManager and sessionContext were the last beans loaded.
Then pull a HibernateGrailsPlugin.groovy from https://github.com/grails-plugins/grails-hibernate-plugin/commits/master/HibernateGrailsPlugin.groovy (for your version of grails) and put it in the default package. Add <plugins><plugin>HibernateGrailsPlugin</plugin></plugins> to your grails.xml.
Next I made a copy of GORMEnhancingBeanPostProcessor.groovy and explicitly call configureDomainOnly() on the grailsConfigurator. There may be a better location for this, but I had already done the work of replacing this class as I was debugging. (Code sample is in the next comment)
I'm still having trouble with HibernatePluginSupport (won't find my dataSource), I'll add a comment once I resolve that.
I solved it, unfortunately I had to make changes to HibernatePluginSupport and GORMEnhancingBeanPostProcessor. I don't see any way to attach files, and I don't want to take the time to create a fork, so I'll post relevant portions: GORMEnhancingBeanPostProcessor:
try {
GrailsRuntimeConfigurator grailsConfigurator = applicationContext.getBean("grailsConfigurator", GrailsRuntimeConfigurator)
grailsConfigurator.applicationContext = applicationContext
applicationContext = grailsConfigurator.configureDomainOnly()
DomainClassGrailsPlugin.enhanceDomainClasses(application, applicationContext)
HibernatePluginSupport.enhanceSessionFactory(bean, application, applicationContext)
}
HibernatePluginSupport: (in doWithSpring add second part of this expression)
if (getSpringConfig().containsBean('dataSource') || parentCtx?.containsBean('dataSource')) {
(replace shorter enhanceSessionFactory())
static enhanceSessionFactory(SessionFactory sessionFactory, GrailsApplication application, ApplicationContext ctx) {
def datastores = [:]
enhanceSessionFactory(sessionFactory, application, ctx, '', datastores)
ctx.eventTriggeringInterceptor.datastores = datastores
}
Thanks a lot @lavandowska I'll try to add/test your patch sortly
You can contact me at ${my username}@gmail.com directly if you would like me to email you the files.