Mike3355
Mike3355

Reputation: 12061

WEB-INF/config/persistence.xml cannot be opened because it does not exist

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

Upvotes: 0

Views: 1208

Answers (2)

PaulNUK
PaulNUK

Reputation: 5229

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;

    }

Upvotes: 1

Paulius Matulionis
Paulius Matulionis

Reputation: 23413

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.

Upvotes: 1

Related Questions