PersistenceContext and Autowire dependency injection annotations are not working. I get NullPointerException. I have gone around the issue by creating getEntityManager and getDaoInstance methods in their respective Abstract class. However, still, I wish to figure out/understand why the annotations will not work in the first place.
I looked at related contents on StackOverflow, but I had had little luck.
- Spring dependency injection not working
- Inject a EntityManagerFactory through @PersitenceContext or @PersitenceUnit?
- Not able to connect to MySQL - Glassfish and Hibernate
PS: I am new to Java
AbstractDao class with getEntityManager method (Full code, https://pastebin.com/6nKh1ujY)
package com.beetlehand.model.dao;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;
public abstract class AbstractDao<T, S> implements AbstractDaoInterface<T, S> {
protected EntityManager entityManager;
public EntityManager getEntityManager() {
if(this.entityManager == null) {
this.entityManager = Persistence
.createEntityManagerFactory("NewPersistenceUnit")
.createEntityManager();
}
return this.entityManager;
}
// more code below
}
Example of how getEntityManager method called in Dao class (Full code, https://pastebin.com/dhjz17sN)
package com.beetlehand.model.dao;
import com.beetlehand.model.AttributeEntity;
import org.apache.commons.lang3.StringUtils;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class AttributeDao extends AbstractDao<AttributeEntity, Integer> {
public AttributeEntity getById(Integer id) {
if(id == null) return null;
return getEntityManager().find(AttributeEntity.class, id);
}
// more code below
}
AbstractService with getDaoInstance method (Full code, https://pastebin.com/H4HGB43y)
package com.beetlehand.model.service;
import com.beetlehand.model.dao.AbstractDaoInterface;
import org.apache.commons.lang3.text.WordUtils;
import java.lang.reflect.Constructor;
import java.util.HashMap;
public class AbstractService {
protected HashMap<String, Object> daoInstance = new HashMap<>();
public AbstractDaoInterface getDaoInstance(String className) {
try {
if(!daoInstance.containsKey(className)) {
className = WordUtils.capitalizeFully(className, new char[]{'_'})
.replaceAll("_", "");
Class<?> clazz = Class.forName("com.beetlehand.model.dao." + className);
Constructor<?> ctr = clazz.getConstructor();
daoInstance.put(className, ctr.newInstance());
}
}catch(Exception ex) {
ex.printStackTrace();
return null;
}
return (AbstractDaoInterface) daoInstance.get(className);
}
}
Example of how getDaoInstance method called in Service class (Full code, https://pastebin.com/86yNSmsX)
package com.beetlehand.model.service;
import com.beetlehand.model.*;
import com.beetlehand.model.dao.AbstractDaoInterface;
public class CatalogService extends AbstractService {
public AttributeEntity addAttribute(AttributeEntity attributeEntity) {
AbstractDaoInterface attributeDao = this.getDaoInstance("attribute_dao");
attributeDao.saveRow(attributeEntity);
return attributeEntity;
}
public AttributeValueEntity addAttributeValue(AttributeValueEntity attributeValueEntity) {
AbstractDaoInterface attributeValueDao = this.getDaoInstance("attribute_value_dao") ;
attributeValueDao.saveRow(attributeValueEntity);
return attributeValueEntity;
}
// more code below
}