0

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!

Community
  • 1
  • 1
dreTa
  • 27
  • 1
  • 3

4 Answers4

0

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>
Ranga Reddy
  • 2,936
  • 4
  • 29
  • 41
  • That is not the correct answer I am afraid. Notice '@PersistenceContext' on the EntityManager. The OP knows it has to be injected, but he wants to do it differently, but it did not work :) – Rafal G. May 01 '15 at 10:35
  • I try this but I get another error: "Problem accessing /rest/myresource/todos. Reason: SERVICE_UNAVAILABLE" – dreTa May 01 '15 at 16:36
0

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.

Rafal G.
  • 4,252
  • 1
  • 25
  • 41
0
  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;

pegas
  • 111
  • 1
  • 9
0

Try to use @PersistenceContext on EntityManager itself,

@PersistenceContext
Private EntityManager entityManager;

I am not that it works on setter method