简体   繁体   English

初始化EntityManager类对象,该怎么办?

[英]Initialization of EntityManager class object, how to do it?

I have following abstract dao class with me: I am using find(Long primaryKey) method for getting data. 我有以下抽象的dao类:我正在使用find(Long primaryKey)方法获取数据。 like 喜欢

public abstract class AbstractDao<T> {
    static final Logger logger = Logger.getLogger(AbstractDao.class);

    @PersistenceContext
    private EntityManager entityManager;

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

    public T find(Long primaryKey) {
        //Here entityManager is null therefore I am getting null pointer exception
        return entityManager.find(entityClass,primaryKey);
    }
}        

Please suggest some techniques to intitalize entityManager object. 请提出一些初始化entityManager对象的技术。

You can remove PersistenceContext annotation from EntityManager and create next abstract method 您可以从EntityManager删除PersistenceContext批注并创建下一个抽象方法

public abstract void setEntityManager(EntityManager entityManager);

In this way you can put next method in the main class 这样,您可以将下一个方法放在主类中

@PersistenceContext(unitName = "HERE YOU HAVE TO PUT NAME OF ENTITY MANAGER FACTORY")
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

and all will be works ;) I have it in my owner DAO and all works 都将成为作品;)我将其保存在我的所有者DAO和所有作品中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM