0

This is not a duplicate of Same question but not for a webapp

My persistence.xml file is located in wepapp/WEB-INF/config/persistence.xml

I use annotations to bring it in, I did it this way because I am using hibernate and JPA 2 at the same time. I am cleaning out hibernateTemplate but need them to work together for the moment.

@Configuration
@EnableJpaRepositories("request.repositoryJPA")
@ComponentScan("request.domain, classes.applicant")
@PropertySource("classpath:WEB-INF/config/persistence.xml")
@EnableTransactionManagement
public class JpaConfiguration {

Full stack trace:

Caused By: java.io.FileNotFoundException: class path resource [WEB-INF/config/persistence.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:153)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:90)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72)
    at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58)
    Truncated. see log file for complete stacktrace
Mike3355
  • 11,305
  • 24
  • 96
  • 184

2 Answers2

1

I personally wouldn't bother using a persistence.xml if I was using Spring boot, but if I was I'd load it like this:

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html (see the 73.8 Use a traditional persistence.xml).

Why are you using a PropertySource to try and load it ?

Here's how to create one programmatically, no persistence.xml involved (you'd have to adapt it for hibernate):

 public LocalContainerEntityManagerFactoryBean MyPersistenceUnit() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(aDataSourceYouveCreatedElsewhere);
        em.setPackagesToScan("some package name");
        em.setLoadTimeWeaver(loadTimeWeaver);
        em.setMappingResources("META-INF/orm.xml");

        EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
        EclipseLinkJpaDialect dialect = vendorAdapter.getJpaDialect();
        dialect.setLazyDatabaseTransaction(true);

        vendorAdapter.setDatabasePlatform(environment.getProperty("some soft coded property"));
        vendorAdapter.setGenerateDdl(false);
        vendorAdapter.setShowSql(Boolean.parseBoolean(environment.getProperty("some handy property to turn on and off showing sql")));

        em.setPersistenceUnitName("whatveryouwanttocallit");
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        return em;

    }
PaulNUK
  • 4,774
  • 2
  • 30
  • 58
  • This is a legacy project and I am trying to use JPA and Hibernate to gracefully work together. This is my current solution and I am not using spring-boot I am using `4.2.4.RELEASE` I would have loved if I could use spring-boot in this project! – Mike3355 Jun 06 '16 at 15:50
  • I've changed my answer to demonstrate how to do it without a need for persistence.xml. Of great use is the packages to scan property , which you use instead of listing every entity in the xml file. Moving to a programmatic approach is much better in the long run, as apart from anything else it's refactor friendly. – PaulNUK Jun 07 '16 at 08:03
  • 1
    This helped but the root cause was Weblogic. By Default it loaded a javax.persistence.jar file version 1 BUT I need the updated jar file to work with hibernate 4 so I had to edit the `PRE_CLASS` in the config file. JPA 2.1 does not work with old javax.persistence.jar files. However thank you for helping me get rid of more XML as that is always my goal! This had me confused for several days. – Mike3355 Jun 09 '16 at 01:29
1

wepapp/WEB-INF/ is not in the classpath. In web application the classpath is classes directory in WEB-INF folder which is in a .war file.

That's an example of my project where persitence.xml is placed:

enter image description here

And then I reference it using: classpath*:META-INF/persistence.xml. So what you need to do is to make sure your persitence.xml file is in the classpath.

If it's maven project just put it somewhere under resources directory of your project and it will be automatically included in the classpath. For e.g. resources/config/persistence.xml and then reference classpath:config/persistence.xml. Otherwise make sure that your persistence.xml file ends up in the classes directory when the project is built.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • Yes I am using maven. I put the persistence.xml file in `resources` run `mvn clean install` and then ran the project and got the same error. I have confirmed the `.xml` file is in the classes directory in the `target` folder. – Mike3355 Jun 06 '16 at 16:44
  • I had to update my `@PropertySource("classpath:persistence.xml")` and it worked sides the `:org.xml.sax.SAXParseException:Document root element "persistence", must match DOCTYPE root "null".` error – Mike3355 Jun 06 '16 at 16:48