javax.el.PropertyNotFoundException:目标无法访问,'null'在外键引用上返回null

时间:2012-07-16 06:03:06

标签: jsf foreign-keys jpa-2.0

我知道managedBean有问题,但我无法弄清楚它是什么

当我从jsf页面尝试departmentManagedBean.department.faculty.facultyName时,它会给出一个如下的空指针:

  

javax.el.PropertyNotFoundException:/administrator/department/DepartmentRegistration.xhtml @ 30,260 value =“#{departmentRequestBean.department.faculty.facultyName}”:目标无法访问,'null'返回null

javax.el.PropertyNotFoundException: /administrator/department/DepartmentRegistration.xhtml @30,260 value="#{departmentRequestBean.department.faculty.facultyName}": Target Unreachable, 'null' returned null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at org.richfaces.component.util.InputUtils.findConverter(InputUtils.java:123)
at org.richfaces.component.util.InputUtils$1.getConverterByProperty(InputUtils.java:56)
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:148)
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:137)
at org.richfaces.renderkit.InputRendererBase.getConvertedValue(InputRendererBase.java:50)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:636)

public class Department implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "DepartmentID")
    private Integer departmentID;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "DepartmentName")
    private String departmentName;
    @JoinColumn(name = "FacultyName", referencedColumnName = "FacultyName")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    public Faculty faculty;

    public Faculty getFaculty() {
        return faculty;
    }

    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }
    public Department() {

    }

    public Department(Integer departmentID) {
        this.departmentID = departmentID;
    }

    public Department(Integer departmentID, String departmentName) {
        this.departmentID = departmentID;
        this.departmentName = departmentName;
    }

    public Integer getDepartmentID() {
        return departmentID;
    }

    public void setDepartmentID(Integer departmentID) {
        this.departmentID = departmentID;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

public class DepartmentSessionBean {
    @PersistenceContext(unitName = "ApplicationPU")
    private EntityManager em;

    public void persist(Department department) {     
        em.persist(department);
    }
}

public class Faculty implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "FacultyID")
    private Integer facultyID;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "FacultyName")
    private String facultyName;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty", fetch = FetchType.EAGER)
    private List<Department> departmentList;

    public Faculty() {
    }

    public Faculty(Integer facultyID) {
        this.facultyID = facultyID;
    }

    public Faculty(Integer facultyID, String facultyName) {
        this.facultyID = facultyID;
        this.facultyName = facultyName;
    }

    public Integer getFacultyID() {
        return facultyID;
    }

    public void setFacultyID(Integer facultyID) {
        this.facultyID = facultyID;
    }

    public String getFacultyName() {
        return facultyName;
    }

    public void setFacultyName(String facultyName) {
        this.facultyName = facultyName;
    }

    public List<Department> getDepartmentList() {
        return departmentList;
    }

    public void setDepartmentList(List<Department> departmentList) {
        this.departmentList = departmentList;
    }
}

这是我需要解决方案的ManagedBean:

@ViewScoped public class DepartmentRequestBean {
@EJB
private DepartmentSessionBean departmentSessionBean;
private Department department = new Department();
public Department getDepartment() {
    return department;
}

public void setDepartment(Department department) {
    this.department = department;
}


public void save(){


        departmentSessionBean.persist(department);

}

public DepartmentRequestBean() {
    this.faculty = new Faculty();
}}

2 个答案:

答案 0 :(得分:8)

  

value =“#{departmentRequestBean.department.faculty.facultyName}”:目标无法访问,'null'返回null

这基本上意味着以下值之一

  • #{departmentRequestBean.department}
  • #{departmentRequestBean.department.faculty}

在处理表单提交期间返回null。由于您自己明确创建了Department,因此它只能表示Faculty仍为null

要正确修复,请在DepartmentRequestBean

中替换
private Department department = new Department();

通过

private Department department;

@PostConstruct
public void init() {
    department = new Department();
    department.setFaculty(new Faculty());
}

答案 1 :(得分:0)

以这种方式在支持bean中初始化你的部门类

public Department getDepartment() {
if(department == null){
department = (Department)super.getInstance(Department.class);
}
return department;
}

如果对象变为空,这将创建一个新的部门对象