0

I am using Maven+Spring+jpa for building a web based application. I am using AbdtractDao class using EntityManager as follows

@SuppressWarnings("unchecked")
public abstract class AbstractDao<T> {
    static final Logger logger = Logger.getLogger(AbstractDao.class);
    @PersistenceContext(unitName = "entityManager")
    private EntityManager entityManager;

    private Class<T> entityClass;

    public AbstractDao(Class<T> entityClass) {

        this.entityClass = entityClass;
        logger.info("####################### Inside constructor"+entityClass);
    }

    public AbstractDao() {
    }

    protected EntityManager getEntityManager() {
        return this.entityManager;
    }

    public void create(T entity) {
        this.entityManager.persist(entity);
    }

    public void edit(T entity) {
        this.entityManager.merge(entity);
    }

    public void remove(T entity) {
        this.entityManager.remove(this.entityManager.merge(entity));
    }

    public T find(Long primaryKey) {
        return this.entityManager.find(entityClass, primaryKey);
    }

    public List<T> findAll() {
        CriteriaQuery cq = this.entityManager.getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return this.entityManager.createQuery(cq).getResultList();
    }

}

When I call find method it gives me null pointer exception: My Dao class is:

@Repository
public class CurrentUserDaoImpl extends AbstractDao<CurrentUser>{

}

Service Is:

@Service
public class CurrentUserServiceImpl implements CurrentUserService{
    CurrentUser currentUser = null;

    private CurrentUserDaoImpl currentUserDao = new CurrentUserDaoImpl();

    @Override
    @Transactional
    public void insertCurrentUser(CurrentUser currentUser) {

    }

    @Override
    public CurrentUser getCurrentUser(Long userId) {
        currentUser = currentUserDao.find(userId);
        return currentUser;
    }

}

Service Interface is:

      public interface CurrentUserService {
            public void insertCurrentUser(CurrentUser currentUser);
            public CurrentUser getCurrentUser(Long userId);
        }

And calling point is:

    CurrentUser currentUser = new CurrentUser();
    currentUser = currentUserService.getCurrentUser(userId);

Please suggest some solution.. Insertion is working fine, but it is giving error in getting data only.

Masudul
  • 21,823
  • 5
  • 43
  • 58
Swapnil Walivkar
  • 295
  • 3
  • 5
  • 15

1 Answers1

0

You need to pass CurrentUser class to AbstractDao class. Using super you should pass it.

@Repository
public class CurrentUserDaoImpl extends AbstractDao<CurrentUser>{

    public CurrentUserDaoImpl(){
      super(CurrentUser.class);
    }

}

Otherwise the entityClass of AbstractDao will be null.

public abstract class AbstractDao<T> {
  // You need to initialize this field from sub class.
  private Class<T> entityClass;

   public AbstractDao(Class<T> entityClass) {
    this.entityClass = entityClass;
   }
 }
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • I think, debug is now only solution – Masudul Nov 20 '13 at 08:48
  • Itried dibugging.It is giving NPE on return this.entityManager.createQuery(cq).getResultList(); line of following method: public T find(Long primaryKey) { logger.info("<<<<<<<<<<<<<<<<<<<<<<<<<,, Entity class is: "+entityClass); logger.info("--------------------------,, primary key is: "+primaryKey); return this.entityManager.find(entityClass, primaryKey); } – Swapnil Walivkar Nov 20 '13 at 08:56
  • Perhaps log is fine, class object is printing and primary key is also printing.I can't figure out where I am going wrong – Swapnil Walivkar Nov 20 '13 at 09:01
  • Sory, My bad. Actual line is : this.entityManager.find(entityClass, primaryKey); – Swapnil Walivkar Nov 20 '13 at 09:07
  • It shows that entitymanager class object is null, how to initialize the entity manager class? – Swapnil Walivkar Nov 20 '13 at 09:21
  • That is initialize at `persistence.xml`. Look at that file and see what is wrong. – Masudul Nov 20 '13 at 09:52
  • EntityManager while insertion operation is not null but for retrieval operation it is null. Why so? – Swapnil Walivkar Nov 20 '13 at 10:18