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,543
  • 5
  • 40
  • 55
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,543
  • 5
  • 40
  • 55