w_pingfan 2009-07-15 10:28
浏览 467
已采纳

spring整合hiberante如何取得设置的hibernateProperties

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种方法.

  • 写回答

8条回答 默认 最新

  • wanghaolovezlq 2009-07-15 13:08
    关注

    那看来不能这么注入,

    这种方式是看来只能通过getBean("&sessionFactory")的方式来得到工厂对象本身,没办法,
    只能实现ApplicationContextAware之类的接口得到整个ioc容器,再得到它了,

    如果是web应用的话,那可以用org.springframework.web.context.support.WebApplicationContextUtils来得到ApplicationContext,就不用依赖spring的接口了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大