PDA

View Full Version : hibernate-config.xml is not populated correctly



amorgovsky
Dec 21st, 2006, 07:42 AM
I have a datasource bean entry in the hibernate-config.xml, in which I specify entries like username and password. I would like these entries to get resolved by their values which are specified in a Spring environment properties file which I have on the classpath. However, in the runtime logs I see that username, password, and the other properties are not getting resolved. I thought that the ${} notation would work. May someone please tell me what I am doing wrong here? Thanks.

spring-properties.properties:

datasource.username=alex
datasource.password=xyz123

hibernate-config.xml:

<beans>
<bean id="datasource" class="x"
...
...
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
</bean>
</beans>

momania
Dec 21st, 2006, 07:51 AM
Can you show us how you load your properties within your spring configuration?

amorgovsky
Dec 21st, 2006, 08:01 AM
I use the Properties object and getProperties(). I am the build engineer so I do not know much more than this for now, but I understand that we use standard getter/setter methods.

momania
Dec 21st, 2006, 08:12 AM
I use the Properties object and getProperties(). I am the build engineer so I do not know much more than this for now, but I understand that we use standard getter/setter methods.

Ok, but some technical background would be usefull. ;)

Is your hibernate-config.xml file a real hibernate xml file or a spring xml file. The filename used is a little bit confusing.

If it's a spring xml you should see something like


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="location" value="somevalue"/>
</bean>


That's the part of the spring config that loads the property file.

amorgovsky
Dec 21st, 2006, 08:19 AM
Well, the Hibernate config file is a Hibernate XML config file, but the Spring file is not an XML file, it is is a Java Properties file with key,value pairs, like:

name=alex
password=xyz123

Thanks.

momania
Dec 21st, 2006, 08:31 AM
Because you use <bean> tags in you hibernate-config.xml I still suspect it is a spring configuration file and not an actual hibernate configuration file.

Where is it that you load the spring-properties.properties file?

amorgovsky
Dec 21st, 2006, 08:35 AM
I have the Spring properties file in a JAR which I have on the classpath.

Chris Lee
Dec 21st, 2006, 09:25 AM
Have you configured a PropertyPlaceholderConfigurer - this is (one of) the Spring beans responsible for processing the placeholders ${} in the Spring context file(s).

Note that this won't touch your hibernate config file; for that, you may wish to consider configuration via Spring (via org.springframework.orm.hibernate3.LocalSessionFac toryBean or org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean)

amorgovsky
Dec 21st, 2006, 09:36 AM
Thanks, I will try this.

amorgovsky
Dec 29th, 2006, 09:24 PM
Thank you, we have this working now by putting in the code suggested. Now, the problem is that everything is working when the Spring properties file is on the classpath, but now when it is placed in a JAR, the properties file is not resolved. Thus, I basically would like to store this properties file with the <key,value> pairs in a JAR file, and then have Spring read the file from this JAR. I am using Weblogic for the application server. Do you have any suggestions as to what I should do? Thanks.