Two Namingstrategies for one SessionFactory in Spring
I am using hibernate 3.5 and Sp开发者_如何学Goring 3.0.4.
I have some old tables and some new tables that need to different NamingStrategies. I declare a sessionFactory in spring with a namingStrategy;
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="energyDataSource" />
<property name="hibernateProperties">
<value>
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
<property name="namingStrategy" ref="namingStrategy"/>
<property name="annotatedClasses">
<list>
<!-- user stuff -->
<value>user.model.UserAccount</value>
<!-- energy -->
<value>com.energy.domain.Selskapstype</value>
</list>
</property>
I want the namingstrategy to kick in for UserAccount, but not for Selskapstype. Is there a way to do this? Either with annotation or xml?
Sure, just write your own implementation of NamingStrategy
which delegates to one of several other strategies, depending on the table name, and then plug that custom strategy into your SessionFactory
.
Could you just use two different sessionFactories? One for the old tables and one for the new tables? This way, you can set different naming strategies for each sessionfactory.
Have different dao classes for each sessionfactory and inject accordingly.
cheers
So this is what I ended up doing. I created at dataAccessContext.xml that is loaded on startup with contextConfigLocation. This includes one of the sessionFactories.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
<property name="namingStrategy" ref="namingStrategy"/>
...
</bean>
In my web.xml I include OpenSessionInViewFilter which maps to /*
<filter>
<filter-name>HibernateSessionRequestFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
Then the other sessionFactory is included in dataAccessContextEnergy.xml which is imported in my spring config energy-config.xml.
Energy-config.xml also includes an opensessioninviewfilterinterceptor;
<mvc:interceptors>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactoryEnergy"/>
</bean>
So then I have two sessionFactories and two open sessions... I hope.
Is this a good way of doing it? Or will I burn in hell? :-)
精彩评论