0

We are trying to convert EJB based application to completely spring based application. In the process, I am facing NullPointerException while trying to access Hibernate\JPA entity manager. Below are the code snippets:

DAO class

@Transactional(propagation = Propagation.REQUIRES_NEW)
public class ExampleDAOBean implements IExampleDAOBean {

    @PersistenceContext(unitName = "PersonInfo")
    private EntityManager entityManager = null;
    
    @Override
    public List<PersonPersist> findAll(String sourceType) {
        List<PersonPersist> persistList = new ArrayList<PersonPersist>();
        Query query = getEntityManager().createNamedQuery(PersonPersist.FIND_ALL);
        try {
            persistList = (List<PersonPersist>) query.getResultList();
        } catch (NoResultException noResultException) {
        }
        return persistList;
    }
    
    @Override
    public void setEntityManager(EntityManager em) {
        this.entityManager = em;

    }

    @Override
    public EntityManager getEntityManager() {
        return this.entityManager;
    }
    
}

persistence.xml :

<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="PersonInfo" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>paymentsDS</jta-data-source>
        <mapping-file>typedef.hbm.xml</mapping-file>
        <class>
            com.persist.PersonPersist
        </class>
        <properties>
            <property name="hibernate.cfg_xml_file"
                value="/hibernate.cfg.xml" />
            <property name="tomee.jpa.cdi" value="false" />
        </properties>
    </persistence-unit>
    
</persistence>

I have only DAO and service bean definitions in context.xml. Could anyone please point out what is the mistake I have done.

Edit: The stack shows NullPointerException in DAO class at the below line: Query query = getEntityManager().createNamedQuery(PersonPersist.FIND_ALL);

No other extra information is captured in the stack trace.

Mounika
  • 11
  • 3
  • Can you share the stack trace of that error and the code where you try to access entity manager? As an example are you by chance creating the DAO via `new ExampleDAOBean()`? – Thomas Sep 14 '22 at 13:07
  • I am injecting DAO Bean into service class from context.xml file like: – Mounika Sep 14 '22 at 13:25
  • Btw, why aren't you using Spring repositories which also support JPA if you're already using Spring? – Thomas Sep 14 '22 at 14:00
  • Sorry, I am not aware of Spring Repositories. Initially, we had EJB model, so the entityManager was getting injected by the container. Just wanted to remove the EJB dependency with minimal changes as it is legacy code. – Mounika Sep 14 '22 at 14:57
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – knittl Oct 07 '22 at 23:13

0 Answers0