【发布时间】:2010-12-26 16:38:39
【问题描述】:
我正在开发一个使用 hibernate 作为 ORM 的 Java Web 应用程序。是否可以将 Hibernate.cfg.xml 与 applicaion-config.xml 合并?
【问题讨论】:
-
来自 Spring 的应用程序配置文件???
标签: xml hibernate spring configuration
我正在开发一个使用 hibernate 作为 ORM 的 Java Web 应用程序。是否可以将 Hibernate.cfg.xml 与 applicaion-config.xml 合并?
【问题讨论】:
标签: xml hibernate spring configuration
这是可能的,因为在幕后,当您按如下方式设置 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 自己的连接提供程序。
问候,
【讨论】:
您可以将 spring 与休眠一起使用。 您只需为您的休眠配置创建一个 bean,并使用 aop 或 bean 名称将其加载到您的应用程序启动中。
这是一个教程:http://www.roseindia.net/struts/hibernate-spring/spring-context.shtml 还有一个:http://www.ibm.com/developerworks/web/library/wa-spring2/
【讨论】: