如何交换hibernate.cfg.xml属性?
我有一个Hibernate配置文件如何交换hibernate.cfg.xml属性?,hibernate,orm,properties,configuration-files,Hibernate,Orm,Properties,Configuration Files,我有一个Hibernate配置文件Hibernate.cfg.xml其中有硬编码的属性名,如: <hibernate-configuration> <session-factory> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mysecret
Hibernate.cfg.xml
其中有硬编码的属性名,如:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysecretpassword</property>
...
</session-factory>
</hibernate-configuration>
我该怎么做?对于Spring中的数据源,我可以在我的应用程序context.xml中使用这个:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
Hibernate的等价物是什么?如果这是一个选项,您可以从Hibernate.cfg.xml
中删除用户名和密码,并在类路径上的Hibernate.properties
文件中声明它们。但是您确实需要从XML配置文件中删除它们,因为它会覆盖“遗留”属性文件中的属性。从文件中:
解决问题的另一种方法
配置是指定一个完整的
在名为
hibernate.cfg.xml
。此文件可以是
用于替换
hibernate.properties
文件或,如果两者都存在
存在,以替代属性
如果这不是一个选项(如果您不能在Spring配置文件中配置Hibernate),那么您必须在构建时使用构建工具(Ant、Maven等)中的一些过滤功能来处理它。配置对象有一个readProperties方法(我不知道您描述了什么)。如果您确实计划使用自定义属性或覆盖hibernate.cfg.xlm,请确保首先调用configure(),然后再调用setProperty或setProperties。从hibernate.cfg.xml中删除配置参数并声明以下内容:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">${jdbc.username}</property>
<property name="hibernate.connection.password">${jdbc.password}</property>
...
</session-factory>
</hibernate-configuration>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
</value>
</property>
</bean>
类路径:jdbc.properties
hibernate.dialogue=${hibernate.dialogue}
Ok。我把“hibernate.properties”文件放在我的“/src/java”目录中,现在它可以工作了。谢谢:)但是我想把jdbc.properties和hibernate.properties放在一个文件中(因为它们的用户名和密码设置相同)。有可能这样做吗?因为使用“PropertyPlaceHolderConfigure”的“location”属性,我无法访问“/src/java”。我被限制在不同级别的“/web”文件夹中。@Benny:一些问题:1。为什么hibernate不使用数据源?2.为什么不使用Spring配置hibernate?嗨,Pascal。这是一个很好的问题。我这样做是因为我对冬眠和春天很陌生。因此,我必须先尝试一些东西。拥有两个独立的配置文件是我找到的最快的解决方案。如果我想使用Hibernate的数据源,那么我必须引用它,对吗?在一些hibernate配置中,我看到了类似NameOfDataSourceBean的设置。我将检查这是否有效…我应该将jdbc.properties放在哪里?jdbc.properties-文件名\n jdbc.username是jdbc.properties中的一行,如jdbc.username=john。因此,它与jdbc不同