Issue
I'm struggling to configure my Spring application using different .properties files for hibernate. My plan was to create an maven property that would reflect which environment I want to choose, selected via profile. And then on mine hibernate.cfg.xml would load the properties variables.
File: hibernate.cfg.xml:
.... <property name="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</property> ...
File: persistence-dev.properties:
... hibernate.hbm2ddl.auto = create ...
File: spring-context.xml:
... <context:property-placeholder location="classpath*:*persistence-${envTarget}.properties" /> ...
What am I missing here?
Solution
hibernate.cfg.xml
is not managed by Spring.
Trying to use PropertyPlaceholderConfigurer
(or it's replacement PropertySourcesPlaceholderConfigurer
) in order to filter it is useless.
What you can do is use Maven resource filtering support:
<profiles>
<profile>
<id>dev</id>
<properties>
<hibernate.hbm2ddl.auto>dev</hibernate.hbm2ddl.auto>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<hibernate.hbm2ddl.auto>validate</hibernate.hbm2ddl.auto>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
You can also use it in conjunction with Spring profiles support (assuming you are willing to pass Spring profile name to Maven during build time, not only using runtime, which you will do anyway if you run Spring profiles related tests from Maven):
<profile>
<activation>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
<properties>
<hibernate.hbm2ddl.auto>dev</hibernate.hbm2ddl.auto>
</properties>
</profile>
...
Answered By - Ori Dar