3

我有一个实体试图调用与 DAO 对话的外观(我称之为步骤)。只有在这种情况下,当我观察/设置断点时,实体管理器才为空(违规行 >> http://goo.gl/rqyRg )。对 Java 还是有点陌生​​,希望能从你们中收集到一些东西,让球朝着正确的调试方向发展。现在,我怀疑问题出在我自己的头骨厚度上。

这是有问题的门面。<< StepsFacade.java >>

package com.mdjdreview.session;

import com.mdjdreview.dao.Steps;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless
public class StepsFacade extends AbstractFacade<Steps> {
    @PersistenceContext(unitName = "MdJdReviewPU2")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public StepsFacade() {
        super(Steps.class);
    }

    public Object findAllRange(int low, int high) {
        try {
            Query q = this.em.createNamedQuery("Steps.findAllRange");
            q.setMaxResults(high);
            q.setFirstResult(low);
            return q.getResultList();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    public Object findByPatternRange(String title, int low, int high) {
        try {
            Query q = this.em.createNamedQuery("Steps.findByPatternRange");
            q.setParameter("title", title.toUpperCase() + "%");
            q.setMaxResults(high);
            q.setFirstResult(low);
            return q.getResultList();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    public int findByPatternRangeCount(String title) {
        Query q = this.em.createNamedQuery("Steps.findByPatternRangeCount");
        q.setParameter("title", title.toUpperCase() + "%");
        return ((Long) q.getSingleResult()).intValue();
    }

    public Object findByTitle(String title) {
        try {
            return this.em.createNamedQuery("Steps.findByTitle").setParameter("title", title).getSingleResult();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    public Object findByProductId(Object productId) {

        try {
            return this.em.createNamedQuery("Steps.findByProductId").setParameter("productId", productId).getResultList();
        } catch (Exception e) {
            System.out.println("----------- findByProductId err "+e.getLocalizedMessage());
            System.out.println("----------- findByProductId err "+e.getMessage());
            System.out.println("----------- findByProductId err "+e.getStackTrace());
            System.out.println(productId);
            return e.getMessage();
        }
    }

    public int findByProductQuestionCount(Object productId) {
        Query q = this.em.createNamedQuery("Steps.findByProductQuestionCount");
        q.setParameter("productId", productId);

        if(q.getSingleResult() == null) {
            return 0;
        }
        else {
            return ((Long) q.getSingleResult()).intValue();
        }
    }

    public int findBySlideStepCount(Object slidesId) {
        Query q = this.em.createNamedQuery("Steps.findBySlideStepCount");
        q.setParameter("slidesId", slidesId);

        if(q.getSingleResult() == null) {
            return 0;
        }
        else {
            return ((Long) q.getSingleResult()).intValue();
        }
    }

    public int findByCategoryStepCount(Object categoriesId) {
        Query q = this.em.createNamedQuery("Steps.findByCategoryStepCount");
        q.setParameter("categoriesId", categoriesId);

        if(q.getSingleResult() == null) {
            return 0;
        }
        else {
            return ((Long) q.getSingleResult()).intValue();
        }
    }

    public int findByModuleStepCount(Object modulesId) {
        Query q = this.em.createNamedQuery("Steps.findByModuleStepCount");
        q.setParameter("modulesId", modulesId);

        if(q.getSingleResult() == null) {
            return 0;
        }
        else {
            return ((Long) q.getSingleResult()).intValue();
        }
    }

    public Object findByExamStep(Object step, Object generatedExamsId) {
        try {
            Query q = this.em.createNamedQuery("Steps.findByExamStep");
            q.setParameter("step", step);
            q.setParameter("generatedExamsId", generatedExamsId);
            q.setMaxResults(1);
            q.setFirstResult(0);
            return q.getSingleResult();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    /**
     * @param em the em to set
     */
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
}

而且,令人费解的是,这里有一个代码几乎相同的 Facade,它运行良好。<< AnswersFacade.java >>

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mdjdreview.session;

import com.mdjdreview.dao.Answers;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author nick
 */
@Stateless
public class AnswersFacade extends AbstractFacade<Answers> {
    @PersistenceContext(unitName = "MdJdReviewPU2")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AnswersFacade() {
        super(Answers.class);
    }

    public Object findByQuestionId(Object questionId) {
        try {
            return this.em.createNamedQuery("Answers.findByQuestionId").setParameter("questionId", questionId).getResultList();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    public Object findAllRange(int low, int high) {
        try {
            Query q = this.em.createNamedQuery("Answers.findAllRange");
            q.setMaxResults(high);
            q.setFirstResult(low);
            return q.getResultList();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    public Object findByPatternRange(String title, int low, int high) {
        try {
            Query q = this.em.createNamedQuery("Answers.findByPatternRange");
            q.setParameter("title", title.toUpperCase() + "%");
            q.setMaxResults(high);
            q.setFirstResult(low);
            return q.getResultList();
        }
        catch(NoResultException ex) {
            return null;
        }
    }

    public int findByPatternRangeCount(String title) {
        Query q = this.em.createNamedQuery("Answers.findByPatternRangeCount");
        q.setParameter("title", title.toUpperCase() + "%");
        return ((Long) q.getSingleResult()).intValue();
    }
}

这是调用门面的实体,<< ExamResults.java >>

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mdjdreview.entities;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

// I need to import this package to allow for access to the category object
import com.mdjdreview.dao.Steps;
import com.mdjdreview.dao.Categories;
import com.mdjdreview.session.StepsFacade;
import com.mdjdreview.session.CategoriesFacade;
import com.mdjdreview.session.ProductToCategoriesFacade;
import com.mdjdreview.session.ProductToStepsFacade;

import java.util.List;
import java.util.Map;

import javax.ejb.EJB;

/**
 *
 * @author joshua
 * this is where we layout the graph
 * 
 */
@ManagedBean
@ViewScoped
public class ExamResults implements Serializable {

    private CartesianChartModel categoryModel;
    private PieChartModel pieModel;

    @EJB
    private StepsFacade stepsFacade = new StepsFacade();

    @EJB
    private CategoriesFacade categoryFacade = new CategoriesFacade();

    public ExamResults() {
        createCategoryModel();
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }
     // this one draws out the graph

    private void createCategoryModel() {
        categoryModel = new CartesianChartModel();

        Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        // TODO: this is not called eamxId, this is a productId don't forget to change this
        String productId = (String) requestMap.get("productId");

        System.out.println("--------------------------- prouctID = "+productId);


        List<Categories> cats;

        List<Steps> steps;

        try {
            steps = (List<Steps>)this.stepsFacade.findByProductId((Object)productId);
            int itemCount = steps.size();
            System.out.println("setps in list = "+itemCount);
        } catch (Exception e) {
            System.out.println("no steps for you");
            System.out.println(e.getMessage());
        }

        try {

            cats = this.categoryFacade.findByTypeParentId((Object)productId, "question");
            int itemCount = cats.size();
            System.out.println("cats in list = "+itemCount);

        } catch (Exception e) {
            System.out.println("no, dice on the categories");
        }

        //


        System.out.println("just tried to pull steps what's above this line?");


        ChartSeries correct = new ChartSeries();
        correct.setLabel("Correct");
        // what they got right
        // setp 1: 
            // <LIST> Step pull back by the steps facade
            // <LIST> Keywords
            // <LIST> Catnames 

        // step 2: loop through steps, query category ID

        // that 90 there for heart is a magic number, that needs to be pulled in dynamically
        // it looks lie in the AnswersToCategories dao, we can pull the 

        correct.set("Heart", 90);
        correct.set("Bacteria", 100);
        correct.set("Mitral Valve", 44);
        correct.set("Pediatrics", 80);
        correct.set("OBGYN", 25);

        ChartSeries missed = new ChartSeries();
        missed.setLabel("Missed");
        // what they got wrong
        missed.set("Heart", 10);
        missed.set("Bacteria", 0);
        missed.set("Mitral Valve", 56);
        missed.set("Pediatrics", 20);
        missed.set("OBGYN", 75);
// auto create x & y
        categoryModel.addSeries(correct);
        categoryModel.addSeries(missed);
    }   
}

这是我迄今为止尝试解决的问题:

  1. 我倾注了应用程序服务器(GlassFish)的启动输出,没有看到任何东西,每个人都很高兴,没有警告,错误或任何更高的东西,只是一堆信息。
  2. 我已经查看了整个 Stack Overflow 并且很多代码看起来与此相同,并且无法吓到任何线索。
  3. 我想,从我的实体来看,希望外墙(http://goo.gl/MSl8F)应该被注入并尝试不实例化新的外墙对象(如下),但这只会产生更多问题(外墙在断点)。
@EJB
private StepsFacade stepsFacade;

@EJB
private CategoriesFacade categoryFacade;

有任何想法吗?感谢您的关注。

4

1 回答 1

6

这个不对:

@EJB
private StepsFacade stepsFacade = new StepsFacade();

@EJB
private CategoriesFacade categoryFacade = new CategoriesFacade();

public ExamResults() {
    createCategoryModel();
}

这里有2个主要错误:

  1. 您不应该自己实例化 EJB。容器对此负责。你应该让它们通过容器注入@EJB。容器还将注意每个 EJB 依次通过@PersistenceContext.
  2. 您不应该尝试在构造函数中使用 EJB。那时它们没有被注入。在构造实例之前,在 Java 中不可能设置实例变量。您应该改为使用@PostConstruct. 容器会在构造和依赖注入后直接调用它。

这是正确的:

@EJB
private StepsFacade stepsFacade;

@EJB
private CategoriesFacade categoryFacade;

@PostConstruct
public void init() { // Note: method name is fully to your choice.
    createCategoryModel();
}

也可以看看:

于 2013-05-23T15:27:48.727 回答