EJB - 无法从ServiceLayer执行结果

时间:2015-05-25 09:08:55

标签: java jpa ejb ejb-3.0

当我从DAO层(ProjectDAOImpl)执行时,我能够看到数据库的结果。

然而,当我从服务层的结果执行时,我得到空指针异常。

  

显示java.lang.NullPointerException   ProjectServiceImpl.fetchProjects(ProjectServiceImpl.java:25)   ProjectServiceImpl.main(ProjectServiceImpl.java:36)

如何解决此问题?

DAO图层界面和类

@Remote
public interface ProjectDAO {


    List<Project> fetchProjectDetail();
}

@Stateless
public class ProjectDAOImpl implements ProjectDAO {
    private EntityManagerFactory entityManagerFactory = 
        Persistence.createEntityManagerFactory("projects");    

    public ProjectDAOImpl() {
    }

    public List<Project> fetchProjectDetail() {
        List<Project> listProject = new ArrayList<Project>();
            listProject = 
                    getEntityManager().createNamedQuery("Project.findProjects").getResultList();
        return listProject;
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
}

服务层接口和类

@Remote
public interface ProjectService {

    List<Project> fetchProjectDetail();
}

@Stateless
public class ProjectServiceImpl implements ProjectService {
    private ProjectDAO projectDAO;

    public ProjectServiceImpl() {
    }

    @EJB
    public void setProjectDAO(ProjectDAO projectDAO) {
        this.projectDAO = projectDAO;
    }

    @GET
    @Path("/projects")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Project> fetchProjects() {
    return getProjectDAO().fetchProjectDetail();
    }

    public ProjectDAO getProjectDAO() {
        return projectDAO;
    }

    public static void main(String [] args) {
        ProjectServiceImpl projectServiceImpl = new ProjectServiceImpl();
        projectServiceImpl.fetchProjects();

    }
}

0 个答案:

没有答案