0

Is it possible for me to have two different persistence.xml files under META-INF eg. persistence-one.xml and persistence-two.xml and then somehow use <property name="persistenceXmlLocation" value="${db.persistence.file.name}"/> in my spring-context.xml to use the appropriate one using property files ?

I am doing this because I have two separate environments with different configurations - providers/dialects etc.

If I do above changes then I get Caused by: java.io.FileNotFoundException: and it's not able to read the appropriate file.

How I can make this work ?

Spring 3, Hibernate 5.3

Naxi
  • 1,504
  • 5
  • 33
  • 72
  • 2
    Initially, I wanted to post an answer telling you how to configure it with Maven. Then, however, I noticed that you're using Spring. In that case, `persistence.xml` is no longer required - follow [this tutorial](https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa) on how to configure JPA with Spring. Once you do that, what you want can be achieved by simply providing two Spring profiles, each with the properties specific to one of your environments. – crizzis Apr 25 '19 at 14:04
  • @crizzis thanks . I have moved all of my properties from persistence.xml to spring-context.xml. Now the only missing part is provider details. How do I pass provider into spring-context.xml file ? Once this is done, I will be happily deleting my persistence.xml file – Naxi Apr 25 '19 at 15:13
  • 1
    I'm assuming you're not using Spring Boot. If by 'provider details', you mean the `` declaration in `persistence.xml`, then the equivalent property in `spring-context.xml` is the `LocalContainerEntityManagerFactoryBean.jpaVendorAdapter` (either `EclipseLinkJpaVendorAdapter` or `HibernateJpaVendorAdapter` can be used) – crizzis Apr 25 '19 at 20:57
  • I am having spring 3 with Hibernate 5. So specifying HibernateJpaVendorAdapter in LocalContainerEntityManagerFactoryBean gives an error 'lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence'. – Naxi Apr 26 '19 at 04:19
  • Is it possible to read in persistence.xml from a property file ? If that's possible then that might solve my issue for now. But I haven't got success trying it. It doesn't read the value ${db.someproperty} like this. – Naxi Apr 26 '19 at 04:24
  • See my answer (sorry, I simply didn't realize you were using such an old version of Spring) – crizzis Apr 26 '19 at 08:39

1 Answers1

1

Whoaa... spring 3 with hibernate 5 is going to be... challenging.

In any case, I'd try using <property name="persistenceProvider" value="org.hibernate.jpa.HibernatePersistenceProvider" /> instead of jpaVendorAdapter.

You might want to take a look at the bean's javadoc to see which other properties might be relevant (alternatively, you can use the persistenceXmlLocation property and load all the properties from either persistence_dev.xml or persistence_prod.xml)

crizzis
  • 9,978
  • 2
  • 28
  • 47
  • Thanks Crizzis. I solved it at the moment using two separate persistence.xml files and then pick up the appropriate one using profiles in pom.xml. So yes, persistenceXmlLocation works. :-) – Naxi Apr 26 '19 at 09:02