运行我的应用时出现以下错误:
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
我的persistence.xml是:
<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>
我的Spring配置类:
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;
}
}
我像这样运行AppConfig类:
new AnnotationConfigApplicationContext(AppConfig.class);
运行我的应用的主app类。
我不明白这一点,因为我的persistence.xml位于正确的位置(META-INF
)。我还将hibernate-core.jar放在lib文件夹中,如this个答案之一所示......我也非常确定我的pom.xml
的所有依赖关系都是正确的。
我按照this教程。
有什么建议吗?提前谢谢!
答案 0 :(得分:1)
正如Simon在comments中所建议的那样,将persistence.xml
从src/main/java/META-INF
移动到src/main/resources/META-INF
可以解决问题。