我有一个实体,它试图调用与 DAO 交谈的外观(我称之为步骤)。当我观察/设置断点时,实体管理器仅在这种情况下为空(违规行 >> http://goo.gl/rqyRg )。对 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;@Statelesspublic 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; }}而且,令人费解的是,这里有一个外观几乎相同的代码,但运行良好。>/* * 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 */@Statelesspublic 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(); }}这是调用 Facade 的实体,>/* * 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 objectimport 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@ViewScopedpublic 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); }}这是我迄今为止尝试修复的内容: 我已经查看了应用程序服务器 (GlassFish) 的启动输出,但没有看到任何内容,每个人都很高兴,没有警告、错误或任何更高的信息,只是一堆信息。 我已经查看了 Stack Overflow 的所有内容,很多代码看起来都与此相同,并且没有发现任何线索。 我想,从我的实体来看,希望 Facades ( http://goo.gl/MSl8F ) 应该被注入(inject)并尝试不实例化新的 Facade 对象(以下),但这只会产生更多问题(断点时 Facades 为空)。@EJBprivate StepsFacade stepsFacade;@EJBprivate CategoriesFacade categoryFacade;有任何想法吗?谢谢你看。 最佳答案 这是不对的:@EJBprivate StepsFacade stepsFacade = new StepsFacade();@EJBprivate CategoriesFacade categoryFacade = new CategoriesFacade();public ExamResults() { createCategoryModel();}这里有2个主要错误: 您不应该自己实例化 EJB。容器负责它。您应该让它们通过 @EJB 由容器注入(inject)。容器还将注意每个 EJB 依次通过 @PersistenceContext 注入(inject)正确的实体管理器。 您不应该尝试在构造函数中使用 EJB。那时它们不会被注入(inject)。在 Java 中不可能在构造实例之前设置实例变量。您应该改用 @PostConstruct 。容器将在构建和依赖注入(inject)后直接调用它。 这是对的:@EJBprivate StepsFacade stepsFacade;@EJBprivate CategoriesFacade categoryFacade;@PostConstructpublic void init() { // Note: method name is fully to your choice. createCategoryModel();}也可以看看: NullPointerException while trying to access @EJB bean in managed bean constructor关于java - 在 JSF 中使用 Facades 模式,仅在一个实体中,实体管理器为 Null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16717514/ 10-12 01:31