JPA Web项目上的UserTransaction NullPointerException

时间:2016-06-30 20:40:50

标签: java jsf jpa

我使用IBM RSA作为IDE,而我的服务器是IBM WebSphere。实体类和实体控制器已由RSA自动从DB生成。

但是在utx.begin()行上,抛出" NullPointerException"

实际上在此错误之前,我在EntityManagerFactory上获得了相同的异常,但我现在在构造函数方法中解决了这个问题

我不明白

这是我的经理班:

flags.None

StackTrace:

import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import javax.persistence.PersistenceUnit;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;
import entities.Raporlar;
import java.util.List;
import javax.persistence.Query;
import java.text.MessageFormat;
import java.util.ArrayList;
import javax.ejb.EJBContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
@SuppressWarnings("unchecked")
@JPAManager(targetEntity = entities.Raporlar.class)
public class RaporlarManager {
    @PersistenceUnit
    private EntityManagerFactory emf;
    @Resource
    private UserTransaction utx;
    public RaporlarManager() {
        emf = Persistence.createEntityManagerFactory("new");
        }
    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    @Action(Action.ACTION_TYPE.CREATE)
    public void createRaporlar(Raporlar raporlar) throws Exception {
        EntityManager em = getEntityManager();
        try {
            utx.begin();
            em.joinTransaction();
            em.persist(raporlar);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception e) {
                ex.printStackTrace();
                throw e;
            }
            throw ex;
        } finally {
            em.close();
        }
    }
    @Action(Action.ACTION_TYPE.DELETE)
    public void deleteRaporlar(Raporlar raporlar) throws Exception {
        EntityManager em = getEntityManager();
        try {
            utx.begin();
            em.joinTransaction();
            raporlar = em.merge(raporlar);
            em.remove(raporlar);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception e) {
                ex.printStackTrace();
                throw e;
            }
            throw ex;
        } finally {
            em.close();
        }
    }
    @Action(Action.ACTION_TYPE.UPDATE)
    public void updateRaporlar(Raporlar raporlar) throws Exception {
        EntityManager em = getEntityManager();
        try {
            utx.begin();
            em.joinTransaction();
            raporlar = em.merge(raporlar);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception e) {
                ex.printStackTrace();
                throw e;
            }
            throw ex;
        } finally {
            em.close();
        }
    }
    @Action(Action.ACTION_TYPE.FIND)
    public Raporlar findRaporlarById(long id) {
        Raporlar raporlar = null;
        EntityManager em = getEntityManager();
        try {
            raporlar = (Raporlar) em.find(Raporlar.class, id);
        } finally {
            em.close();
        }
        return raporlar;
    }
    @Action(Action.ACTION_TYPE.NEW)
    public Raporlar getNewRaporlar() {

        Raporlar raporlar = new Raporlar();

        return raporlar;
    }
    @NamedQueryTarget("getRaporlar")
    public List<Raporlar> getRaporlar() {
        EntityManager em = getEntityManager();
        List<Raporlar> results = null;
        try {
            Query query = em.createNamedQuery("getRaporlar");
            results = (List<Raporlar>) query.getResultList();
        } finally {
            em.close();
        }
        return results;
    }
    /**
     * Retrieve a list of entities that can be used to populate a combo box. The
     * MessageFormat can be used to format how your list items will be
     * displayed.
     */
    public List<SelectItem> getRaporlarSelectList() {
        List<Raporlar> raporlarList = getRaporlar();
        List<SelectItem> selectList = new ArrayList<SelectItem>();
        MessageFormat mf = new MessageFormat("{0}");
        for (Raporlar raporlar : raporlarList) {
            selectList.add(new SelectItem(raporlar, mf.format(
                    new Object[] { raporlar.getId() }, 
                    new StringBuffer(), null).toString()));
        }
        return selectList;
    }
}

0 个答案:

没有答案