PDA

View Full Version : NHibernate Configuration for SchemaExport


malte
07-28-2007, 02:31 AM
Hi,

I would like to use the NHibernate 1.2 SchemaExport tool to create my database. This works basically as follows:

NHibernate.Cfg.Configuration myConfiguration;
NHibernate.Tool.hbm2ddl.SchemaExport se =
new NHibernate.Tool.hbm2ddl.SchemaExport(myConfigurati on);
se.Create(true, true);

Since I manage all other NHibernate stuff via Spring, I don't actually have the required Configuration object at hand. I use the following Spring configuration to create my session factory:

<object id="MySessionFactory"
type="Spring.Data.NHibernate.LocalSessionFactoryObject,
Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Foo.Persistence</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2005Dialect"/>
<entry key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
<entry key="hibernate.current_session_context_class"
value="managed_web" />
</dictionary>
</property>
</object>


The factory is injected into my class as follows:

<object name="My_Persistence.Maintenance"
type="Foo.Persistence.Maintenance, Foo.Persistence">
<property name="SessionFactory" ref="MySessionFactory"/>
</object>

Given this spring configuration, I thought I could just get the NHibernate Configuration out of the LocalSessionFactoryObject.Configuration property. However, MySessionFactory is not a LocalSessionFactoryObject, it's actually an instance of NHibernate.Impl.SessionFactoryImpl. This puzzles me.

Its NHibernate.ISessionFactory interface does not provide the Configuration, and now I don't know where to get it from. Is there a way to get hold of the NHibernate Configuration via Spring, or do I have to instantiate it manually?

Malte

malte
07-28-2007, 03:34 AM
However, MySessionFactory is not a LocalSessionFactoryObject, it's actually an instance of NHibernate.Impl.SessionFactoryImpl. This puzzles me.


Looks like I've understood it now: Objects of type IFactoryObject that are not injected directly but used to create the object that is injected. In this case, LocalSessionFactoryObject creates a NHibernate.Impl.SessionFactoryImpl. If you want to get hold of the "real" object, use the ampersand prefix:

<object name="My_Persistence.Maintenance"
type="Foo.Persistence.Maintenance, Foo.Persistence">
<property name="SessionFactory" ref="&amp;MySessionFactory"/>
</object>

The rest of the question is hereby solved, since I can now access the LocalSessionFactoryObject.Configuration as planned.

Malte