javax.el.PropertyNotFoundException:目标无法访问,'teacher'返回null

时间:2015-08-19 08:46:44

标签: java spring hibernate hibernate-criteria

我是新手,对Eng不好 现在我有问题当我在这里插入新行时返回null是错误。

INFO: javax.el.PropertyNotFoundException: /index.xhtml @143,70 value="#{studentCtrl.student.teacher.teachId}": Target Unreachable, 'teacher' returned null
javax.el.PropertyNotFoundException: /index.xhtml @143,70 value="#{studentCtrl.student.teacher.teachId}": Target Unreachable, 'teacher' returned null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:552)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at javax.faces.component.UIForm.visitTree(UIForm.java:381)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:399)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:263)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1193)
    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:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, 'teacher' returned null
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:124)
    at org.apache.el.parser.AstValue.getType(AstValue.java:58)
    at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:168)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 44 more

ส.ค. 19, 2558 3:26:02 หลังเที่ยง com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: javax.el.PropertyNotFoundException: /index.xhtml @143,70 value="#{studentCtrl.student.teacher.teachId}": Target Unreachable, 'teacher' returned null
javax.faces.FacesException: javax.el.PropertyNotFoundException: /index.xhtml @143,70 value="#{studentCtrl.student.teacher.teachId}": Target Unreachable, 'teacher' returned null
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:270)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1193)
    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:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.PropertyNotFoundException: /index.xhtml @143,70 value="#{studentCtrl.student.teacher.teachId}": Target Unreachable, 'teacher' returned null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:552)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at javax.faces.component.UIForm.visitTree(UIForm.java:381)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:399)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:263)
    ... 27 more
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, 'teacher' returned null
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:124)
    at org.apache.el.parser.AstValue.getType(AstValue.java:58)
    at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:168)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 44 more

ส.ค. 19, 2558 3:26:02 หลังเที่ยง com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
SEVERE: javax.el.PropertyNotFoundException: /index.xhtml @143,70 value="#{studentCtrl.student.teacher.teachId}": Target Unreachable, 'teacher' returned null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:552)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at javax.faces.component.UIForm.visitTree(UIForm.java:381)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1740)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:399)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:263)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1193)
    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:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, 'teacher' returned null
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:124)
    at org.apache.el.parser.AstValue.getType(AstValue.java:58)
    at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:168)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 44 more

我的学生模特

package model;
// Generated Aug 19, 2015 9:19:39 AM by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * Student generated by hbm2java
 */
@Entity
@Table(name = "student", schema = "public")
public class Student implements java.io.Serializable {

    private int stuId;
    private Teacher teacher;
    private String stuName;
    private Integer stuAge;

    public Student() {
    }

    public Student(int stuId) {
        this.stuId = stuId;
    }

    public Student(int stuId, Teacher teacher, String stuName, Integer stuAge) {
        this.stuId = stuId;
        this.teacher = teacher;
        this.stuName = stuName;
        this.stuAge = stuAge;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "stu_id", unique = true, nullable = false)
    public int getStuId() {
        return this.stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "stu_teachid")
    public Teacher getTeacher() {
        return this.teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Column(name = "stu_name", length = 40)
    public String getStuName() {
        return this.stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name = "stu_age")
    public Integer getStuAge() {
        return this.stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

}

对于教师模型

package model;
// Generated Aug 19, 2015 9:19:39 AM by Hibernate Tools 3.4.0.CR1

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Teacher generated by hbm2java
 */
@Entity
@Table(name = "teacher", schema = "public")
public class Teacher implements java.io.Serializable {

    private int teachId;
    private String teachName;
    private Set<Student> students = new HashSet<Student>(0);

    public Teacher() {
    }

    public Teacher(int teachId) {
        this.teachId = teachId;
    }

    public Teacher(int teachId, String teachName, Set<Student> students) {
        this.teachId = teachId;
        this.teachName = teachName;
        this.students = students;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "teach_id", unique = true, nullable = false)
    public int getTeachId() {
        return this.teachId;
    }

    public void setTeachId(int teachId) {
        this.teachId = teachId;
    }

    @Column(name = "teach_name", length = 40)
    public String getTeachName() {
        return this.teachName;
    }

    public void setTeachName(String teachName) {
        this.teachName = teachName;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "teacher")
    public Set<Student> getStudents() {
        return this.students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }
}

我的道

package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import model.Student;
import model.Teacher;

@Repository
public class StudentDaoImpl implements StudentDao{

    /**
     * 
     */
    private static final long serialVersionUID = 8024106490842552958L;

    List<Student> studentList ;
    List<Teacher> teacherList ;

    @Autowired
    SessionFactory sessionFactory ;


    @SuppressWarnings("unchecked")
    public List<Student> studentList() {
        // TODO Auto-generated method stub
        Session session = sessionFactory.getCurrentSession() ;
        Criteria criteria = session.createCriteria(Student.class , "student") ;
        criteria.setFetchMode("student.teacher", FetchMode.JOIN) ;
        criteria.createAlias("student.teacher", "teacher") ;


        return criteria.list() ;
    }


    @Override
    public boolean delete(Student student) {
        try{
            sessionFactory.getCurrentSession().delete(student);
            return true ;
        }catch(Exception e){
            return false ;
        }
    }


    @Override
    public void studentUpdate(Student student) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().saveOrUpdate(student);
    }



    @Override
    public List<Student> studentFind(Student student) {
        // TODO Auto-generated method stub
        return sessionFactory.getCurrentSession().createCriteria(Student.class)
                .add(Restrictions.like("stuName", "%"+student+"%")).list();
    }
}

包裹控制器;

import java.util.ArrayList; import java.util.List;

import javax.annotation.PostConstruct;

import model.Student; import model.Teacher;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;

导入service.StudentSer;

@Controller
public class StudentCtrl {
    List<Student> studentList ;
    List<Student> showStudent ;
    List<Student> findShudent ;
    Student student ;
    Teacher teacher ;

    @Autowired
    StudentSer studentSer ;

    @PostConstruct
    public void init(){
        student = new Student() ;
        studentList = new ArrayList<Student>() ;
        showStudent = studentSer.studentList() ;
    }
    public  void save(){
        studentSer.studentSave(student);
        init();
    }
    public void update(Student student){
        this.student = student ;
        studentSer.studentUpdate(student);
        init();
    }
    public void delete(Student student){
        studentSer.delete(student) ;
        init(); 
    }
    public void find(){
        studentSer.studentFind(student) ;
    }
    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public List<Student> getShowStudent() {
        return showStudent;
    }

    public void setShowStudent(List<Student> showStudent) {
        this.showStudent = showStudent;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public StudentSer getStudentSer() {
        return studentSer;
    }

    public void setStudentSer(StudentSer studentSer) {
        this.studentSer = studentSer;
    }
}

这是Xhmtl

<h:form id="addStudent">
            <h:panelGrid columns="3" cellpadding="5">
                <h:outputLabel for="name" value="Name:" style="font-weight:bold" />
                <p:inputText id="name" value="#{studentCtrl.student.stuName}" />
                <br />
                <h:outputLabel for="name" value="Age:" style="font-weight:bold" />
                <p:inputText id="age" value="#{studentCtrl.student.stuAge}" />
                <br />
                <h:outputLabel for="name" value="Teacher Name:"
                    style="font-weight:bold" />
                <h:selectOneMenu value="#{studentCtrl.student.teacher.teachId}" >
                    <f:selectItems value="#{teacherCtrl.showTeacher}" var="show"
                        itemLabel="#{show.teachName}"
                        itemValue="#{show}" />
                </h:selectOneMenu>
                <br/>
                <p:commandButton value="Save" actionListener="#{studentCtrl.save}"
                    process="addStudent" update="studbtb" icon="ui-icon-check" />
                <p:commandButton value="Clear" type="reset" />
            </h:panelGrid>
        </h:form>

如何解决问题,Thxq大家^^

0 个答案:

没有答案