Java 没有 EntityManagerFactory 类型的合格 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20809788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No qualifying bean of type EntityManagerFactory
提问by Lurk21
I am running a Spring rest app that is a console app. I'm using spring-boot which embeds tomcat 7.
我正在运行一个 Spring rest 应用程序,它是一个控制台应用程序。我正在使用嵌入 tomcat 7 的 spring-boot。
When I boot the app, I get a stack trace and dumped out. Here's the root cause:
当我启动应用程序时,我得到一个堆栈跟踪并转储出来。这是根本原因:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.jav
a:559)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:515)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProce
ssor.java:682)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProces
sor.java:655)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:353)
... 22 more
Here's where I start my Application:
这是我开始我的应用程序的地方:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Here's my /src/main/resources/spring-config.xml:
这是我的/src/main/resources/spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.mydomain.orm" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:/persistence.xml" />
<property name="persistenceUnitName" value="userPersistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="POSTGRESQL" />
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="username" value="myuser" />
<property name="password" value="" />
</bean>
</beans>
Here's my DAO:
这是我的 DAO:
@Repository("userDao")
@Transactional(propagation = Propagation.REQUIRED)
public class UserDAO {
private static final String SELECT_QUERY = "select u from users";
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void insert(User user) {
entityManager.persist(user);
}
public User load(int id) {
return entityManager.find(User.class, id);
}
public List<User> selectAll() {
Query query = entityManager.createQuery(SELECT_QUERY);
@SuppressWarnings("unchecked")
List<User> users = (List<User>) query.getResultList();
return users;
}
}
And my /src/main/persistence.xml:
还有我的/src/main/persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="userPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
<class>com.mydomain.orm.User</class>
</persistence-unit>
</persistence>
采纳答案by Lurk21
SO keeps telling me that this is a trivial answer and converting it to a comment, so here's a long line in hopes of convincing SO that this is not a trivial answer, but in fact a lot of work went into it.
SO一直告诉我这是一个微不足道的答案并将其转换为评论,所以这里有很长的一条线,希望说服SO这不是一个微不足道的答案,但实际上做了很多工作。
Look here for the answer: Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory
在这里寻找答案:Spring JPA (Hibernate) Noqualifying bean of type: javax.persistence.EntityManagerFactory
回答by Brad
If you're going to use @PersistenceContext, you're required to set up persistence.xml for your use case, since that annotation effectively tells Spring, "Use the Java EE way of setting up JPA, not the Spring/Hibernate way." Keep in mind that Tomcat is not a Java EE container, so it may not work right.. Spring Boot is pretty new, though, so it's possible that there is a way to do this that might not yet be well-documented.
如果您打算使用@PersistenceContext,则需要为您的用例设置 persistence.xml,因为该注释有效地告诉 Spring,“使用 Java EE 设置 JPA 的方式,而不是 Spring/Hibernate 的方式。 ” 请记住,Tomcat 不是 Java EE 容器,因此它可能无法正常工作。. 不过,Spring Boot 是相当新的,因此可能有一种方法可以做到这一点,但尚未有详细记录。
I'd start with setting up persistence.xml and see what happens.
我会从设置persistence.xml 开始,看看会发生什么。