Java 在db-JSF JPA等中更新记录

Java 在db-JSF JPA等中更新记录,java,sql,jsf,jpa,Java,Sql,Jsf,Jpa,我想知道你是否能帮助我 基本上我已经创建了一个DB,它将数据添加到表中的两个数据中,剩下的列是空白的,我想做的是,能够用更多的数据为空白列更新这些记录,我怎样才能做到这一点? 这是我的atm代码,但我只是得到了一个零点错误,不知道我是否做得对 这是u.i <p> Student Number : <!--More for me than anything --> <h:inputTex

我想知道你是否能帮助我

基本上我已经创建了一个DB,它将数据添加到表中的两个数据中,剩下的列是空白的,我想做的是,能够用更多的数据为空白列更新这些记录,我怎样才能做到这一点? 这是我的atm代码,但我只是得到了一个零点错误,不知道我是否做得对

这是u.i

 <p>
                    Student Number : <!--More for me than anything -->
                    <h:inputText value="#{editMarkingBean.markSectionTwo.studentNumber}" />
                </p>
支持bean:

private MarkingService markingService;

@Inject
private MarkingFacade markingFacade;
public void markSectionTwo() {
    this.markingFacade.edit(this.markSectionTwo);
    this.setMessage("Mark Saved");
}
这是创建表的实体

private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String studentNumber,markingStage, markingCompleted, markSectionOne, markSectionTwo, markSectionThree, markSectionFour, markSectionFive, overalMark, plagorism, feedback, comments;
我得到了错误

WARNING:   javax.el.PropertyNotFoundException: /lecturer/marking/marking-section-two.xhtml @109,82 value="#{editMarkingBean.markSectionTwo.markSectionTwo}": Target Unreachable, 'null' returned null
如何根据学生编号更新记录

谢谢各位

编辑

下面是完整的editMarkingController类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sws.control;

import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import sws.business.MarkingService;
import sws.entities.Marking;
import sws.persistance.MarkingFacade;

/**
 *
 * @author Richard
 */
@Named(value = "editMarkingBean")
@ViewScoped
public class EditMarkingController {

    private String searchString;
    private String ordering;
    private String criteria;
    private String match;
    private Date today;
    private String caseMatch;
    private int spinnerField;
    private Marking markSectionOne;
    private Marking studentNumber;
    private Marking markSectionTwo;


    private MarkingService markingService;

    @Inject
    private MarkingFacade markingFacade;

    /*
     public String markSectionOne() {
     //supposing the data in markSectionOne is filled...
     this.markingFacade.create(markSectionOne);
     this.setMessage("Mark Saved");
     //after saving...
     markSectionOne = new Marking();
     // now navigating to the next page
     return "/lecturer/marking/marking-section-two";
     }
     */
    public void editMark() {
        this.markingFacade.edit(this.markSectionTwo);
        this.setMessage("Mark Saved");
    }

    public void markSectionTwo() {
        this.markingFacade.edit(this.markSectionTwo);
        this.setMessage("Mark Saved");
    }

    private void setMessage(String message) {
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, new FacesMessage(message, ""));
    }

    public Marking getMarkSectionTwo() {
        return markSectionTwo;
    }

    public void setMarkSectionTwo(Marking markSectionTwo) {
        this.markSectionTwo = markSectionTwo;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public String getOrdering() {
        return ordering;
    }

    public void setOrdering(String ordering) {
        this.ordering = ordering;
    }

    public String getCriteria() {
        return criteria;
    }

    public void setCriteria(String criteria) {
        this.criteria = criteria;
    }

    public String getMatch() {
        return match;
    }

    public void setMatch(String match) {
        this.match = match;
    }

    public Date getToday() {
        return today;
    }

    public void setToday(Date today) {
        this.today = today;
    }

    public String getCaseMatch() {
        return caseMatch;
    }

    public void setCaseMatch(String caseMatch) {
        this.caseMatch = caseMatch;
    }

    public int getSpinnerField() {
        return spinnerField;
    }

    public void setSpinnerField(int spinnerField) {
        this.spinnerField = spinnerField;
    }

    public Marking getMarkSectionOne() {
        return markSectionOne;
    }

    public void setMarkSectionOne(Marking markSectionOne) {
        this.markSectionOne = markSectionOne;
    }

    public Marking getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(Marking studentNumber) {
        this.studentNumber = studentNumber;
    }

    public MarkingService getMarkingService() {
        return markingService;
    }

    public void setMarkingService(MarkingService markingService) {
        this.markingService = markingService;
    }

    public MarkingFacade getMarkingFacade() {
        return markingFacade;
    }

    public void setMarkingFacade(MarkingFacade markingFacade) {
        this.markingFacade = markingFacade;
    }

}
全套阅卷服务

import java.util.List;
import javax.ejb.EJB;
import javax.inject.Inject;
import sws.entities.Marking;
import sws.entities.ProjectIdea;
import sws.persistance.MarkingFacade;
import sws.persistance.PersonFacade;

/**
 *
 * @author Richard
 */
public class MarkingService {

    @EJB
    private MarkingFacade markingFacade;

    public List<Marking> getAllMarks() {
        return markingFacade.findAll();
    }
}
编辑2:

我添加了一个后构造

@PostConstruct
public void markSectionTwo() {
    this.markingFacade.edit(this.markSectionTwo);
    markSectionTwo = new Marking();
    this.setMessage("Mark Saved");
}
但是现在我得到了错误消息HTTP500错误

javax.servlet.ServletException: WELD-000049 Unable to invoke public void sws.control.EditMarkingController.markSectionTwo() on sws.control.EditMarkingController@44de1491
root cause

org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke public void sws.control.EditMarkingController.markSectionTwo() on sws.control.EditMarkingController@44de1491
root cause

java.lang.reflect.InvocationTargetException
root cause

javax.ejb.EJBException
root cause

java.lang.IllegalArgumentException: Object: null is not a known entity type.
当我尝试加载页面时

编辑3

我已经解决了这个问题,但现在我只能添加记录,我尝试的是合并记录,因此如果studentNumber与表中的相同,则将MarkSection2更新为此值,而不是在db中为其创建新行

private Marking markSectionTwo;

    private MarkingService markingService;

    @Inject
    private MarkingFacade markingFacade;

    @PostConstruct
    public void init() {
        this.markSectionTwo = new Marking();
    }


    public String markSectionTwo() {
        //supposing the data in markSectionOne is filled...
        //markSectionOne.setMarkSectionOne("markSectionOne");
        //markSectionTwo.setMarkSectionTwo("markSectionTwo");
        this.markingFacade.edit(markSectionTwo);
        this.setMessage("Mark Saved");
        //after saving...
        markSectionTwo = new Marking();
        this.setMessage("Mark Saved");
        // now navigating to the next page
        return "/lecturer/marking/marking-section-two";
    }

    private void setMessage(String message) {
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, new FacesMessage(message, ""));
    }
您的错误消息

javax.el.PropertyNotFoundException (...) #{editMarkingBean.markSectionTwo.markSectionTwo}"
基本上说你必须

  • 一个名为editMarkingBean的托管bean
  • 托管bean中名为markSectionTwo的对象具有适当的getter和setter
  • 对象markSectionTwo中的一个属性称为markSectionTwo,具有适当的getter和setter
  • 所以EL想说的是

    editMarkingBean.getMarkSectionTwo().getMarkSectionTwo()
    

    请检查您的所有类,如果可能的话,发布问题中的所有相关部分,例如类名称(全部)、托管bean范围注释、,getter、setter和attributes。

    marksectiontwo.marksectiontwo会得到什么?要更新db表中名为marksectiontwo的列,并调用支持bean marksectiontwo中的类进行编辑,我真的应该将这些名称更改为不同的名称,但bean中的对象称为markToEdit它调用的是代码的这一部分
    public void markSectionTwo(){this.markingFacade.edit(this.markSectionTwo);this.setMessage(“Mark Saved”);}
    markToEdit是用于其他内容的,很抱歉,不应该在那里谢谢,我已经检查了一切,一切都在那里,我有所有相关的getter和setter,并且更新了上面的完整代码,非常好,谢谢!您不需要初始化editMarkingBean.markSectionTwo吗?在你的bean中添加一个用@PostConstruct注释的方法,并在那里初始化:-)嗨,你有一个如何做的例子吗,我对它的内容有点困惑,所以简单地说:
    @PostConstruct public void markSectionTwo(){this.markingFacade.edit(this.markSectionTwo);this.setMessage(“Mark Saved”);}
    谢谢,如果您觉得此问题已得到回答,请将答案设置为已接受
    private Marking markSectionTwo;
    
        private MarkingService markingService;
    
        @Inject
        private MarkingFacade markingFacade;
    
        @PostConstruct
        public void init() {
            this.markSectionTwo = new Marking();
        }
    
    
        public String markSectionTwo() {
            //supposing the data in markSectionOne is filled...
            //markSectionOne.setMarkSectionOne("markSectionOne");
            //markSectionTwo.setMarkSectionTwo("markSectionTwo");
            this.markingFacade.edit(markSectionTwo);
            this.setMessage("Mark Saved");
            //after saving...
            markSectionTwo = new Marking();
            this.setMessage("Mark Saved");
            // now navigating to the next page
            return "/lecturer/marking/marking-section-two";
        }
    
        private void setMessage(String message) {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, new FacesMessage(message, ""));
        }
    
    javax.el.PropertyNotFoundException (...) #{editMarkingBean.markSectionTwo.markSectionTwo}"
    
    editMarkingBean.getMarkSectionTwo().getMarkSectionTwo()