PDA

View Full Version : Spring and NHibernate



gingalala
06-22-2006, 09:41 PM
Anyone out there have an example of how to write codes using Spring with NHibernate as the ORM.

There is not a single good example out there. I am using the latest version of NHibernate (The alpha release) and The stable version of Spring.

One example online says I need Spring.Orm.dll which I can't find anywhere.

if anyone (if at all) uses these in the real world, please help me with an example.

cheers
G

Mark Pollack
06-23-2006, 03:23 PM
Hi,

You can get the Spring.Data.Orm.NHibernate.dll from the nightly build download page - http://www.springframework.net/downloads/nightly/

I am still in the process of migrating the code to the Spring.Integrations cvs module. Once that is complete there will be a seperate build/download/release for NHibernate support.

To get started there is some basic usage/example located under sandbox\test\Spring\Spring.Data.Orm.NHibernate.Int egration.Tests\Data\Orm\NHibernate.

I certainly realise this less that an ideal - hello world tutorial. The next month or so will be a intensive period for data access development and the plan it to provide a "Northwind" hello world example as well of reference documentation of course. Note that I have not tried using the latest alpha, you may have to recompile.

Cheers,
Mark

Dimon
07-03-2006, 10:58 PM
Hi Mark! hope you and your team are doing well and the conference went fine!

I see that old forum's threads are missed there but there were many interesting posts...

that way, i open the question about the store place for NH session during page lifecycle. I mean i want to use long NH session to have lazy loading during page lifecycle. Early, i opened session on BeginRequest event and closed on EndRequest and it was before you integrated NH :)

Now i have started to use this integration and wandering what should i do to have the same ability?

Thanks in advance for a response!
Dmitry

P.S. we are looking forward for the next release of Spring.net 1.1 :)

jnapier
07-10-2006, 08:02 PM
I wondering the same thing since this is how I managed sessions for web applications as well. Session per request model.

Any pointers would be appreciated.

Bruno Baia
07-10-2006, 09:05 PM
I see that old forum's threads are missed there but there were many interesting posts...

Old forum's threads are still there, you just need to change the filter in the display options (by default "Last Month").

-Bruno

Gengis
07-12-2006, 12:20 PM
I use spring and hibernate a lot in java, here is what I did in .NET do make them work together. Let's consider a file named contextDal.xml :

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">

<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<object id="dbProvider" type="Spring.Data.Support.SqlProvider, Spring.Data">
<property name="ConnectionString" value="Data Source=${datasource.connection.server};Initial Catalog=${datasource.connection.database};User Id=${datasource.connection.userId};Password=${data source.connection.password}"/>
</object>

<object id="sessionFactory" type="Spring.Data.Orm.NHibernate.LocalSessionFactoryObje ct, Spring.Data.Orm.NHibernate">
<property name="DbProvider" ref="dbProvider"/>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
</dictionary>
</property>
<property name="MappingAssemblies">
<list>
<value>MyAssembly</value>
</list>
</property>
</object>

<object id="hibernateTransactionManager" type="Spring.Data.Orm.NHibernate.HibernateTransactionMan ager, Spring.Data.Orm.NHibernate">
<property name="DbProvider" ref="dbProvider"/>
<property name="sessionFactory" ref="sessionFactory"/>
</object>

</objects>

Just replace MyAssembly by your assembly.

Then, write a test class which extend Spring.Data.Orm.NHibernate.HibernateTemplate and inject the sessionFactory in in this class :

<object id="testClass" type="test.TestClass, MyAssembly">
<property name="SessionFactory" ref="sessionFactory"/>
</object>

in a method of your test class, try to run this following piece of code :

IList results = base.Find("select e from app.model.Product ");

where app.model.Product design a class mapped by Hibernate in a cfg.xml file.

For transaction management, let's consider a context file dedicaded to business layer (contextService.xml) :

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">

<object id="serviceTemplate" type="Spring.Transaction.Interceptor.TransactionProxyFac toryObject, Spring.Data" abstract="true">
<property name="PlatformTransactionManager" ref="hibernateTransactionManager"/>
<property name="TransactionAttributes">
<name-values>
<add key="create*" value="PROPAGATION_REQUIRED"/>
<add key="update*" value="PROPAGATION_REQUIRED"/>
<add key="delete*" value="PROPAGATION_REQUIRED"/>
<add key="attach*" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>

<object id="productsManager" type="Spring.Transaction.Interceptor.TransactionProxyFac toryObject, Spring.Data" parent="serviceTemplate">
<property name="target" ref="productsManagerTarget"/>
</object>

<object id="productsManagerTarget" type="foo.service.ProductsManager, MyAssembly">
<!-- additionnal injections -->
</object>
</objects>

Then, everything which happen in a method flagged a transactionnal of a proxied service is embeded in a transaction.

Do not forget to join the 2 context files in a single file (using <import resource="contextDal.xml"/> and <import resource="contextService.xml"/>).

And be carefull to properly embed your mapping file in the assembly at build time (for each hbm file, set the property "BuildAction" at "Embedded Resource").

Last but not least, spring expect nhibernate 1.0.1 so if you use 1.0.2, write this in the app.config :

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="154fdcb44c4484fc" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.0.2.0" newVersion="1.0.2.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

Good luck !

PS : question to the team, is there a schedule for Spring.Data.Orm.NHibernate release ?

Aleksei Kachanov
07-14-2006, 10:31 AM
Spring.Data.Orm.NHibernate