NullPointerException thrown from EntityManager

ε祈祈猫儿з 提交于 2019-12-25 13:59:35

问题


I have a problem with an EntityManager, it throws a NullPointerException. This is the code:

PersonServiceJpa.java

package com.smartpump.service.jpa;

import org.springframework.stereotype.Service;

import com.smartpump.persistent.entity.Person;
import com.smartpump.service.PersonService;

import java.util.List;

import javax.jdo.annotations.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Service("personService")
public class PersonServiceJpa implements PersonService {

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @Override
    public String getNombre() {
        return "Roberto";
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public List<Person> getAll() {
        Query query = entityManager.createNamedQuery("Person.findAll");  <--Here is the problem
        List<Person> persons = null;
        persons = query.getResultList();
        return persons;
    }
}

The code Query query = entityManager.createNamedQuery("Person.findAll"); is where the NullPointerException appears, in the entityManager sentence.

Here are other parts of code.

persistance.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
      <!--   <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
            <property name="datanucleus.appengine.datastoreReadConsistency" value="EVENTUAL" /> 
        </properties> -->
    </persistence-unit>
</persistence>

<!-- 
transaction-type="RESOURCE_LOCAL">
         <class>com.persistent.entity.Person</class>
          -->

applicationContext

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>\WEB-INF\database.properties</value>
        </list>
    </property>
    </bean>

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

    <bean id="personService"
        class="com.smartpump.service.jpa.PersonServiceJpa"></bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"
        lazy-init="true">
        <property name="persistenceUnitName" value="transactions-optional" />
    </bean>
    <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

Thanks a lot!


回答1:


NullPointerException is occurring because of entityManager object is not injected in the PersonServiceJPA class.

In applicationContext.xml you are creating the personService bean. While creating PersonService bean you need inject the entityManager class.

Your Code:

<bean id="personService"
        class="com.smartpump.service.jpa.PersonServiceJpa"></bean>

Modified Code:

<bean id="personService"
        class="com.smartpump.service.jpa.PersonServiceJpa">
    <property name="entityManager" ref="entityManagerFactory"/>
</bean>



回答2:


I think that you either have to set:

<context:annotation-config/>

in your applicationContext.xml, or alternatively define a special Spring bean (also in applicationContext.xml):

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

Please, let me know if it had helped.




回答3:


  1. You should modify your application-context, to add the transaction annotation config.

<beans xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 
	<context:annotation-config/>
  1. You should autowire the EntityManager like so :

    @Autowired

    private EntityManager entityManager;




回答4:


Try to use @PersistenceContext on EntityManager itself,

@PersistenceContext
Private EntityManager entityManager;

I am not that it works on setter method



来源:https://stackoverflow.com/questions/29981562/nullpointerexception-thrown-from-entitymanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!