Problem integrating Grails with existing Spring-Hibernate Java business layer

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem integrating Grails with existing Spring-Hibernate Java business layer

Bob Pawlowski
Hi,

I am trying to integrate my existing Spring-Hibernate based business and persistence layer with Grails.  I use Spring xml files to configure my Hibernate data source, Spring DAOs, and Spring business objects.  I put all of the Spring-Hibernate configuration into the spring/resources.xml file (not sure if this was necessary or if I could use my existing spring xml file and load it into the Grails application).  When I execute "grails run-app", everything seems to load fine, but when I try to execute the first query, it gives the following error:

org.hibernate.hql.ast.QuerySyntaxException: Category is not mapped [select distinct c from Category c where c.parent is null order by c.name]

Category is one of my persistent classes and it looks like it was never mapped into hibernate correctly.  I don't get any sort of error that it can't find the *.hbm.xml files, but only an error when I try to execute the first query. Any help would be appreciated.  Is this the correct approach when integrating existing Spring and Hibernate Java code?  


Here is an excerpt from my resources.xml file (this configuration works in a few other applications that use this business/persistence layer):

        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
                <property name="dataSource" ref="hibernatePool" />

                <property name="mappingResources" >
                        <list>
                                <value>util.hbm.xml</value>
                                <value>pcs.hbm.xml</value>
                                <value>cash.hbm.xml</value>
                                <value>sound.hbm.xml</value>
                                <value>audit.hbm.xml</value>
                                <value>stats.hbm.xml</value>
                        </list>
                </property>
                               
                <property name="hibernateProperties" >
                        <props>
                                <prop key="hibernate.show_sql" >false</prop>
                                <prop key="hibernate.format_sql" >false</prop>
                                <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
                                <prop key="hibernate.connection.autoReconnect" >true</prop>
                                <prop key="hibernate.connection.autoReconnectForPools" >true</prop>
                                <prop key="hibernate.connection.is-connection-validation-required" >true</prop>       
                        </props>
                </property>
               
        </bean>


        <bean name="hibernateCategoryDao" class="com.pcs.dao2.hibernate.HibernateCategoryDao" >
            <property name="sessionFactory" ref="sessionFactory"/>               
        </bean>

        <bean id="categoryManager" class="com.pcs.api.CategoryManagement" >
                <property name="hibernateCategoryDao" ref="hibernateCategoryDao" />
                <property name="jdbcMasterTalkShowDao" ref="jdbcMasterTalkShowDao" />       
        </bean>
Reply | Threaded
Open this post in threaded view
|

Re: Problem integrating Grails with existing Spring-Hibernate Java business layer

Graeme-Rocher
What version of Grails and where did you put your hbm mapping files?

Cheers

On 11/5/07, Bob Pawlowski <[hidden email]> wrote:

>
> Hi,
>
> I am trying to integrate my existing Spring-Hibernate based business and
> persistence layer with Grails.  I use Spring xml files to configure my
> Hibernate data source, Spring DAOs, and Spring business objects.  I put all
> of the Spring-Hibernate configuration into the spring/resources.xml file
> (not sure if this was necessary or if I could use my existing spring xml
> file and load it into the Grails application).  When I execute "grails
> run-app", everything seems to load fine, but when I try to execute the first
> query, it gives the following error:
>
> org.hibernate.hql.ast.QuerySyntaxException: Category is not mapped [select
> distinct c from Category c where c.parent is null order by c.name]
>
> Category is one of my persistent classes and it looks like it was never
> mapped into hibernate correctly.  I don't get any sort of error that it
> can't find the *.hbm.xml files, but only an error when I try to execute the
> first query. Any help would be appreciated.  Is this the correct approach
> when integrating existing Spring and Hibernate Java code?
>
>
> Here is an excerpt from my resources.xml file (this configuration works in a
> few other applications that use this business/persistence layer):
>
>         <bean id="sessionFactory"
> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
>                 <property name="dataSource" ref="hibernatePool" />
>
>                 <property name="mappingResources" >
>                         <list>
>                                 <value>util.hbm.xml</value>
>                                 <value>pcs.hbm.xml</value>
>                                 <value>cash.hbm.xml</value>
>                                 <value>sound.hbm.xml</value>
>                                 <value>audit.hbm.xml</value>
>                                 <value>stats.hbm.xml</value>
>                         </list>
>                 </property>
>
>                 <property name="hibernateProperties" >
>                         <props>
>                                 <prop key="hibernate.show_sql" >false</prop>
>                                 <prop key="hibernate.format_sql" >false</prop>
>                                 <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
>                                 <prop key="hibernate.connection.autoReconnect" >true</prop>
>                                 <prop key="hibernate.connection.autoReconnectForPools" >true</prop>
>                                 <prop key="hibernate.connection.is-connection-validation-required"
> >true</prop>
>                         </props>
>                 </property>
>
>         </bean>
>
>
>         <bean name="hibernateCategoryDao"
> class="com.pcs.dao2.hibernate.HibernateCategoryDao" >
>             <property name="sessionFactory" ref="sessionFactory"/>
>         </bean>
>
>         <bean id="categoryManager" class="com.pcs.api.CategoryManagement" >
>                 <property name="hibernateCategoryDao" ref="hibernateCategoryDao" />
>                 <property name="jdbcMasterTalkShowDao" ref="jdbcMasterTalkShowDao" />
>         </bean>
>
> --
> View this message in context: http://www.nabble.com/Problem-integrating-Grails-with-existing-Spring-Hibernate-Java-business-layer-tf4751409.html#a13586183
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Problem integrating Grails with existing Spring-Hibernate Java business layer

Bob Pawlowski
Thanks for your response.

Version: 1.0 RC1.

I've tried the following locations for my hbm mapping files:
grails-app\conf
grails-app\conf\hibernate
web-app\WEB-INF\classes

Bob

Graeme Rocher wrote:

> What version of Grails and where did you put your hbm mapping files?
>
> Cheers
>
> On 11/5/07, Bob Pawlowski <[hidden email]> wrote:
>  
>> Hi,
>>
>> I am trying to integrate my existing Spring-Hibernate based business and
>> persistence layer with Grails.  I use Spring xml files to configure my
>> Hibernate data source, Spring DAOs, and Spring business objects.  I put all
>> of the Spring-Hibernate configuration into the spring/resources.xml file
>> (not sure if this was necessary or if I could use my existing spring xml
>> file and load it into the Grails application).  When I execute "grails
>> run-app", everything seems to load fine, but when I try to execute the first
>> query, it gives the following error:
>>
>> org.hibernate.hql.ast.QuerySyntaxException: Category is not mapped [select
>> distinct c from Category c where c.parent is null order by c.name]
>>
>> Category is one of my persistent classes and it looks like it was never
>> mapped into hibernate correctly.  I don't get any sort of error that it
>> can't find the *.hbm.xml files, but only an error when I try to execute the
>> first query. Any help would be appreciated.  Is this the correct approach
>> when integrating existing Spring and Hibernate Java code?
>>
>>
>> Here is an excerpt from my resources.xml file (this configuration works in a
>> few other applications that use this business/persistence layer):
>>
>>         <bean id="sessionFactory"
>> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
>>                 <property name="dataSource" ref="hibernatePool" />
>>
>>                 <property name="mappingResources" >
>>                         <list>
>>                                 <value>util.hbm.xml</value>
>>                                 <value>pcs.hbm.xml</value>
>>                                 <value>cash.hbm.xml</value>
>>                                 <value>sound.hbm.xml</value>
>>                                 <value>audit.hbm.xml</value>
>>                                 <value>stats.hbm.xml</value>
>>                         </list>
>>                 </property>
>>
>>                 <property name="hibernateProperties" >
>>                         <props>
>>                                 <prop key="hibernate.show_sql" >false</prop>
>>                                 <prop key="hibernate.format_sql" >false</prop>
>>                                 <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
>>                                 <prop key="hibernate.connection.autoReconnect" >true</prop>
>>                                 <prop key="hibernate.connection.autoReconnectForPools" >true</prop>
>>                                 <prop key="hibernate.connection.is-connection-validation-required"
>>    
>>> true</prop>
>>>      
>>                         </props>
>>                 </property>
>>
>>         </bean>
>>
>>
>>         <bean name="hibernateCategoryDao"
>> class="com.pcs.dao2.hibernate.HibernateCategoryDao" >
>>             <property name="sessionFactory" ref="sessionFactory"/>
>>         </bean>
>>
>>         <bean id="categoryManager" class="com.pcs.api.CategoryManagement" >
>>                 <property name="hibernateCategoryDao" ref="hibernateCategoryDao" />
>>                 <property name="jdbcMasterTalkShowDao" ref="jdbcMasterTalkShowDao" />
>>         </bean>
>>
>> --
>> View this message in context: http://www.nabble.com/Problem-integrating-Grails-with-existing-Spring-Hibernate-Java-business-layer-tf4751409.html#a13586183
>> Sent from the grails - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>    
>
>
>  


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Problem integrating Grails with existing Spring-Hibernate Java business layer

Steven Devijver
Try to add /WEB-INF/ in front of your mapping files names, and places
them in the /WEB-INF/ directory.

Steven

On 11/5/07, Bob Pawlowski <[hidden email]> wrote:

> Thanks for your response.
>
> Version: 1.0 RC1.
>
> I've tried the following locations for my hbm mapping files:
> grails-app\conf
> grails-app\conf\hibernate
> web-app\WEB-INF\classes
>
> Bob
>
> Graeme Rocher wrote:
> > What version of Grails and where did you put your hbm mapping files?
> >
> > Cheers
> >
> > On 11/5/07, Bob Pawlowski <[hidden email]> wrote:
> >
> >> Hi,
> >>
> >> I am trying to integrate my existing Spring-Hibernate based business and
> >> persistence layer with Grails.  I use Spring xml files to configure my
> >> Hibernate data source, Spring DAOs, and Spring business objects.  I put all
> >> of the Spring-Hibernate configuration into the spring/resources.xml file
> >> (not sure if this was necessary or if I could use my existing spring xml
> >> file and load it into the Grails application).  When I execute "grails
> >> run-app", everything seems to load fine, but when I try to execute the first
> >> query, it gives the following error:
> >>
> >> org.hibernate.hql.ast.QuerySyntaxException: Category is not mapped [select
> >> distinct c from Category c where c.parent is null order by c.name]
> >>
> >> Category is one of my persistent classes and it looks like it was never
> >> mapped into hibernate correctly.  I don't get any sort of error that it
> >> can't find the *.hbm.xml files, but only an error when I try to execute the
> >> first query. Any help would be appreciated.  Is this the correct approach
> >> when integrating existing Spring and Hibernate Java code?
> >>
> >>
> >> Here is an excerpt from my resources.xml file (this configuration works in a
> >> few other applications that use this business/persistence layer):
> >>
> >>         <bean id="sessionFactory"
> >> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
> >>                 <property name="dataSource" ref="hibernatePool" />
> >>
> >>                 <property name="mappingResources" >
> >>                         <list>
> >>                                 <value>util.hbm.xml</value>
> >>                                 <value>pcs.hbm.xml</value>
> >>                                 <value>cash.hbm.xml</value>
> >>                                 <value>sound.hbm.xml</value>
> >>                                 <value>audit.hbm.xml</value>
> >>                                 <value>stats.hbm.xml</value>
> >>                         </list>
> >>                 </property>
> >>
> >>                 <property name="hibernateProperties" >
> >>                         <props>
> >>                                 <prop key="hibernate.show_sql" >false</prop>
> >>                                 <prop key="hibernate.format_sql" >false</prop>
> >>                                 <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
> >>                                 <prop key="hibernate.connection.autoReconnect" >true</prop>
> >>                                 <prop key="hibernate.connection.autoReconnectForPools" >true</prop>
> >>                                 <prop key="hibernate.connection.is-connection-validation-required"
> >>
> >>> true</prop>
> >>>
> >>                         </props>
> >>                 </property>
> >>
> >>         </bean>
> >>
> >>
> >>         <bean name="hibernateCategoryDao"
> >> class="com.pcs.dao2.hibernate.HibernateCategoryDao" >
> >>             <property name="sessionFactory" ref="sessionFactory"/>
> >>         </bean>
> >>
> >>         <bean id="categoryManager" class="com.pcs.api.CategoryManagement" >
> >>                 <property name="hibernateCategoryDao" ref="hibernateCategoryDao" />
> >>                 <property name="jdbcMasterTalkShowDao" ref="jdbcMasterTalkShowDao" />
> >>         </bean>
> >>
> >> --
> >> View this message in context: http://www.nabble.com/Problem-integrating-Grails-with-existing-Spring-Hibernate-Java-business-layer-tf4751409.html#a13586183
> >> Sent from the grails - user mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list please visit:
> >>
> >>     http://xircles.codehaus.org/manage_email
> >>
> >>
> >>
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Problem integrating Grails with existing Spring-Hibernate Java business layer

Bob Pawlowski
That did not work, but I figured out what was going on.  I turned on
hibernate tracing.  It turns out that Grails was not using the Hibernate
sessionFactory bean that I configured in resources.xml.  All of the
dao's were using the default Grails sessionFactory.  (Since I'm a new
user to Grails, my analysis may be a little off).  I simply went into
the resource.xml file and change the bean name from sesssionFactory to
sessionFactory1.  After I did this, I saw that my hibernate
sessionFactory was created and all of the hbm.xml files were loaded
properly.  I'm still not sure if the way I did this is the best way.  It
seems that default Grails Hibernate sessionFactory is still being
created.  From the log:

[23015] dialect.Dialect Using dialect: org.hibernate.dialect.HSQLDialect
[23015] dialect.Dialect Using dialect: org.hibernate.dialect.HSQLDialect

My session factory is using MySQL:

[18406] dialect.Dialect Using dialect: org.hibernate.dialect.MySQLDialect
[18406] dialect.Dialect Using dialect: org.hibernate.dialect.MySQLDialect
/
/Is there a way to either hook into the Grails hibernate session factory
or prevent it from being created?  Does it even matter?


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Problem integrating Grails with existing Spring-Hibernate Java business layer

Graeme-Rocher
You can hook into Grails default sessionFactory by providing a
hibernate.cfg.xml file in the grails-app/conf/hibernate dir that
references your different hbm files

Cheers

On 11/5/07, Bob Pawlowski <[hidden email]> wrote:

> That did not work, but I figured out what was going on.  I turned on
> hibernate tracing.  It turns out that Grails was not using the Hibernate
> sessionFactory bean that I configured in resources.xml.  All of the
> dao's were using the default Grails sessionFactory.  (Since I'm a new
> user to Grails, my analysis may be a little off).  I simply went into
> the resource.xml file and change the bean name from sesssionFactory to
> sessionFactory1.  After I did this, I saw that my hibernate
> sessionFactory was created and all of the hbm.xml files were loaded
> properly.  I'm still not sure if the way I did this is the best way.  It
> seems that default Grails Hibernate sessionFactory is still being
> created.  From the log:
>
> [23015] dialect.Dialect Using dialect: org.hibernate.dialect.HSQLDialect
> [23015] dialect.Dialect Using dialect: org.hibernate.dialect.HSQLDialect
>
> My session factory is using MySQL:
>
> [18406] dialect.Dialect Using dialect: org.hibernate.dialect.MySQLDialect
> [18406] dialect.Dialect Using dialect: org.hibernate.dialect.MySQLDialect
> /
> /Is there a way to either hook into the Grails hibernate session factory
> or prevent it from being created?  Does it even matter?
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Problem integrating Grails with existing Spring-Hibernate Java business layer

neilthorne-2
Hi,

we have similar requirements to Bob in that we'd like to reuse an existing Spring/Hibernate architecture that was developed outside of Grails. We don't have any existing Spring web stack however. We just want to reuse our externally defined service/repository/sessionFactory/datasource spring beans.

We have a number of Spring beans advertised in various spring bean xml configuration files that include services, repositories, hibernate sessionFactory and datasource beans. These files are all inside existing JAR artifacts.

I was able to load all of these beans into the Grails application context using import statements in the resource.xml with "classpath:" prefixed uris.

However I did get problems around trying to use our externally defined sessionFactory bean. Our externally configured sessionFactory had that exact bean name and was loaded ok by the top level Grails ApplicationContext.

However then we found that our externally loaded repository beans could not then get access to the sessionFactory bean loaded by Grails - does Grails create a two tier hierarchy of applicationContexts?

Our solution for now is to give Grails what it wants - a sessionFactory with a datasource using the DataSource.groovy config file. We then load our own sessionFactory bean using a different name. This is the same as Bob's solution.

All our beans are then accessible to Controllers using autowiring which is exactly what we wanted, but we end up with a sessionFactory pointing to a hsql in memory database that isn't used. Not particularly clean.

I did notice in the logs that if Grails doesn't find a datasource configured it assumes one will be provided from an external Spring context, but I didn't see the same message for the sessionFactory.

Ideally we'd just be able to define our sessionFactory and dataSource beans in our external configuration, removing the DataSource.groovy file completely and have Grails just use those (without these beans then becoming inaccessible to other externally configured beans).

Is there a standardized way to do this?

IMHO it would be great if Grails supported externally configured sessionFactory and dataSource beans as a first class integration approach. This would help architectural teams like mine to move incrementally towards a Grails based solution.

thanks,

Neil

quote author="Graeme Rocher-2">
Another way is to write your own ContextLoaderListener that subclasses
the GrailsContextLoader creates the Grails ApplicationContext as the
parent then creates your ApplicationContext with the Grails one as the
parent

Cheers

On Jan 18, 2008 1:06 AM, Justin Galzic <jgalzic@gmail.com> wrote:
>
> Figured this out -- we realized that we needed to place all of the spring
> configuration information of the existing app into grails 'resource.xml' so
> that the same spring context is responsible for loading all of the beans.
>
Graeme Rocher-2 wrote
You can hook into Grails default sessionFactory by providing a
hibernate.cfg.xml file in the grails-app/conf/hibernate dir that
references your different hbm files

Cheers

On 11/5/07, Bob Pawlowski <bobp@webcleats.com> wrote:
> That did not work, but I figured out what was going on.  I turned on
> hibernate tracing.  It turns out that Grails was not using the Hibernate
> sessionFactory bean that I configured in resources.xml.  All of the
> dao's were using the default Grails sessionFactory.  (Since I'm a new
> user to Grails, my analysis may be a little off).  I simply went into
> the resource.xml file and change the bean name from sesssionFactory to
> sessionFactory1.  After I did this, I saw that my hibernate
> sessionFactory was created and all of the hbm.xml files were loaded
> properly.  I'm still not sure if the way I did this is the best way.  It
> seems that default Grails Hibernate sessionFactory is still being
> created.  From the log:
>
> [23015] dialect.Dialect Using dialect: org.hibernate.dialect.HSQLDialect
> [23015] dialect.Dialect Using dialect: org.hibernate.dialect.HSQLDialect
>
> My session factory is using MySQL:
>
> [18406] dialect.Dialect Using dialect: org.hibernate.dialect.MySQLDialect
> [18406] dialect.Dialect Using dialect: org.hibernate.dialect.MySQLDialect
> /
> /Is there a way to either hook into the Grails hibernate session factory
> or prevent it from being created?  Does it even matter?
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email