1

I got the following error when I run my app:

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named LOCAL_PERSISTENCE
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:92)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
... 86 more

My persistence.xml is:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

<persistence-unit name="LOCAL_PERSISTENCE">
    <description>Version Control</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.oracle.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@XXXXXXXXXXX" />
        <property name="javax.persistence.jdbc.user" value="system" />
        <property name="javax.persistence.jdbc.password" value="password" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>

</persistence-unit>

My Spring config Class:

package com.mwc.versionControl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.mwc.versionControl.DAO"),
        @ComponentScan("com.mwc.versionControl.service") })
public class AppConfig {

@Bean
public LocalEntityManagerFactoryBean geEntityManagerFactoryBean() {
    LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
    factoryBean.setPersistenceUnitName("LOCAL_PERSISTENCE");
    return factoryBean;
}

@Bean
public JpaTransactionManager geJpaTransactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(geEntityManagerFactoryBean().getObject());
    return transactionManager;
}

}

I run the AppConfig class like this:

new AnnotationConfigApplicationContext(AppConfig.class);

Running the main app class of my app.

I don't understand this as I have my persistence.xml on proper location (META-INF). I also put the hibernate-core.jar on lib folder as suggested in one of this answers... And I'm also pretty sure that all dependencies of my pom.xml are correct.

I follow this tutorial.

Any suggestion? Thanks in advance!

CarlosLeo
  • 123
  • 2
  • 10
  • 1
    Do you have Hibernate in your classpath? – Simon Martinelli Dec 15 '17 at 12:12
  • @SimonMartinelli I'm using intellij (new IDE for me) and now I can see that on my `Project Structure -> My Module -> Dependencies` I have Hibernate somehow broke. It's underlined red and when I put the cursor over it I got this message: `Library 'Hibernate 5.2.12-5.2.12 ' has broken paths.` – CarlosLeo Dec 15 '17 at 12:55
  • So this is the error. You have to fix that first. – Simon Martinelli Dec 16 '17 at 09:47
  • @SimonMartinelli I already did that but I'm still getting the same error... I will digg a little bit more – CarlosLeo Dec 20 '17 at 07:59
  • Now i can see that persistence.xml is not in the directory ../versionControl/out/artifacts/versionControl_war_exploded/META-INF and neither on the ../versionControl/target/versionControl-0.0.1-SNAPSHOT.jar For sure thta's the problem but now I can not figure out why this is happening. I believe my project structure is correct but I will check it out – CarlosLeo Dec 20 '17 at 12:06
  • 1
    your persistence XML should be in the folder src/main/resources/META-INF – Simon Martinelli Dec 20 '17 at 12:24
  • Are you sure about this? I thought it has to be placed on src\main\java\META-INF\persistence.xml directory – CarlosLeo Dec 20 '17 at 13:00
  • No in java only source code will be compiled. No resources are copied from there: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – Simon Martinelli Dec 20 '17 at 13:05
  • Fix all that but I'm still getting same error :-( – CarlosLeo Dec 22 '17 at 10:37

1 Answers1

1

As Simon suggested in the comments, moving persistence.xml from src/main/java/META-INF to src/main/resources/META-INF solves the issue.

gdrt
  • 3,160
  • 4
  • 37
  • 56