Hibernate配置Xml

时间:2009-12-15 05:17:27

标签: xml hibernate spring configuration

我正在使用hibernate作为ORM开发一个java Web应用程序。是否可以将Hibernate.cfg.xml与applicaion-config.xml合并?

2 个答案:

答案 0 :(得分:2)

这是可能的,因为在幕后,当您按如下方式设置SessionFactory时(​​让我们合并hibernate.cfg.xml和applicationContext.xml属性)

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    <property name="mappingResources">
        <list>
            <value>br/com/myApp/domain/User.hbm.xml</value>
            <value>br/com/myApp/domain/Group.hbm.xml</value>
        </list>
    </property>
</bean>

春天会打电话给

// configure load hibernate.cfg.xml from the classpath
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

configuration.addResource("br/com/myApp/domain/User.hbm.xml");
configuration.addResource("br/com/myApp/domain/Group.hbm.xml");

SessionFactory sessionFactory = configuration.buildSessionFactory();

小心LocalSessionFactoryBean API说:

  

可以从Hibernate XML文件(指定为“configLocation”)或完全通过此类读取配置设置。典型的本地配置包括一个或多个“mappingResources”,各种“hibernateProperties”(不是绝对必要的),以及SessionFactory应该使用的“dataSource”。后者也可以通过Hibernate属性指定,但“dataSource”支持任何Spring配置的DataSource,而不是依赖于Hibernate自己的连接提供程序。

的问候,

答案 1 :(得分:0)

你可以使用带休眠的spring。 您只需要为您的hibernate配置创建一个bean,并使用aop或bean名称将其加载到您的应用程序启动中。

这是一个教程:http://www.roseindia.net/struts/hibernate-spring/spring-context.shtml 另一个:http://www.ibm.com/developerworks/web/library/wa-spring2/

相关问题