Spring2.5 整合 Hibernate3 时,我想在dao中取得配置文件中设置的hibernateProperties,找了很久都没找到方法.
配置如下:
[code="java"]
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations"><!-- mappingResources -->
<list>
<value>WEB-INF/classes/com/etone/solid/po/</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SybaseDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
<prop key="use_streams_for_binary">true</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">
auto
</prop>
<!-- prop key="hibernate.hbm2ddl.auto">create-drop</prop-->
<!-- prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.proxool.xml">/WEB-INF/proxool.xml</prop>
<prop key="hibernate.proxool.pool_alias">spring</prop-->
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean
class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>[/code]
谢谢哪位帮忙回答一下
[b]问题补充:[/b]
我拿到的sessionFactory是org.hibernate.SessionFactory,
我看了下,org.springframework.orm.hibernate3.LocalSessionFactoryBean才有getHibernateProperties方法.
我怎么才能得到org.springframework.orm.hibernate3.LocalSessionFactoryBean实例呢?
[b]问题补充:[/b]
to lovewhzlq:
[i]得到工厂本身的话,就注入"&sessionFactory"[/i]
我这样写:
[code="java"]
class="com.etone.common.dao.impl.DaoSupportHibernate3Impl">
[/code]
报错:
[code="java"]
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 207 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The reference to entity "sessionFactory" must end with the ';' delimiter.
[/code]
[color=red]请问这个注入怎么写?[/color]
to xuzhfa123:
[i]Hibernate具体版本号?再查看Spring2.5支持你那个版本不[/i]
整合成功,使用的版本是spring2.5.6 + hibernate3.3.2 + struts 2.1.6
to rain2005:
强制转化应该不行的吧?两个Bena不同.
由于在Dao中使用,我不想通过getBean("sessionFactory")形式来取.
[b]问题补充:[/b]
to lovewhzlq:
我这要写了
[code="java"]
class="com.etone.common.dao.impl.DaoSupportHibernate3Impl">
[/code]
报另一个错误:
[code="java"]
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 210 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '&sessionFactory' is not a valid value for 'NCName'.
[/code]
[b]问题补充:[/b]
我刚才试了一下,找到了一个可以的方法,但感觉不太好..
如下:
在DaoSupportHibernate3Impl中定义增加:
[quote] private SessionFactoryImpl sessionFactoryImpl;
public SessionFactoryImpl getSessionFactoryImpl() {
return sessionFactoryImpl;
}
public void setSessionFactoryImpl(SessionFactoryImpl sessionFactoryImpl) {
this.sessionFactoryImpl = sessionFactoryImpl;
}[/quote]
XML配置:
[code="java"]
class="com.etone.common.dao.impl.DaoSupportHibernate3Impl">
[/code]
在DaoSupportHibernate3Impl中这样使用:
[code="java"]sessionFactoryImpl.getSettings().isScrollableResultSetsEnabled();[/code]
不知道有无更好的方法?
[b]问题补充:[/b]
谢谢各位的热心解答!
我想应该就是各位所说的方法了:
1,注入sessionFactory到org.hibernate.impl.SessionFactoryImpl实例变量
2.通过ApplicationContext来得到.
暂时我是用第1种方法.