java - JPA EntityManager注入失败

时间:2015-12-29 13:50:09

标签: java spring hibernate jpa

我正在尝试完成我的作业,我必须使用Spring构建Restful Webservice。另外我使用JPA(Eclipselink)来编辑,搜索和显示数据库条目。

我的persistence.xml如下;

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="test">
        <class>at.test.entities.UserEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://myserver:3306/somedatabase"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="xxx"/>
            <property name="javax.persistence.jdbc.password" value="xxx"/>
        </properties>
    </persistence-unit>
</persistence>

当我尝试通过

获取entityManager时
entityManager = Persistence.createEntityManagerFactory("test").createEntityManager();

它工作得很好但是如果我想通过@PersistenceContext注释

来做
@PersistenceContext(unitName = "test")
private EntityManager entityManager;

它失败并带有以下堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestHandler': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'test' is defined

工作原理:

@RestController
@RequestMapping(value = "/users")
public class RequestHandler {
    private EntityManager entityManager;

    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<UserTest> test(@RequestParam(value = "asd", defaultValue = "") String name) {
        /* works just fine */
        entityManager = Persistence.createEntityManagerFactory("test").createEntityManager();

        /* Some test stuff */
        UserTest user = entityManager.find(UserTest.class, 1);
        entityManager.getTransaction().begin();
        entityManager.persist(new UserTest("ASDASD", "ASDASdjnwco2eno2oc"));
        entityManager.getTransaction().commit();
        return new ResponseEntity<UserTest>(user, HttpStatus.CREATED);
    }
}

它是如何工作的:

@RestController
@RequestMapping(value = "/users")
public class RequestHandler {
    /* Does not work */
    @PersistenceContext(unitName = "test")
    private EntityManager entityManager;

    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<UserTest> test(@RequestParam(value = "asd", defaultValue = "") String name) {
        /* Some test stuff */
        UserTest user = entityManager.find(UserTest.class, 1);
        entityManager.getTransaction().begin();
        entityManager.persist(new UserTest("ASDASD", "ASDASdjnwco2eno2oc"));
        entityManager.getTransaction().commit();
        return new ResponseEntity<UserTest>(user, HttpStatus.CREATED);
    }
}

感谢您的帮助

解决方案

两种解决方案,AdrianDuta和Branislav Lazic的解决方案都有效。您可以通过XML文件定义bean和持久性单元,也可以按Java-Class配置它们。

虽然我现在使用了这个模板/示例:

spring-boot-mysql-springdatajpa-hibernate

2 个答案:

答案 0 :(得分:2)

使用@PersistenceContext,您将EntityManager bean注入到其余控制器中。要使其有效,您可以定义LocalContainerEntityManagerFactoryBean

@Bean(name = "test")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        // configuration here
}

答案 1 :(得分:2)

在你的applicationContext中定义:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="test" />
</bean>



<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />