繁体   English   中英

更新数据库中的记录-JSF JPA等

[英]updating a record in a db - JSF JPA etc

我想知道你能不能帮我

基本上,我已经创建了一个数据库,它将数据添加到表中的两部分数据中,其余的列留为空白,我想做的是,能够用空白列的更多数据更新这些记录,我怎样才能做到这一点?

这是我的代码atm,但我只是得到一个空点错误,不知道即时通讯是否做对了

这是UI

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

这是输入学生编号的位置,这是我要更新的包含该学生编号的记录(没有办法可以有多个相同的用户名)

                            <p:spinner id="ajaxspinner80-100" value="#{editMarkingBean.markSectionTwo.markSectionTwo}"
                                       stepFactor="1"  min="80" max="100" disabled="#{formBean.number != 8}">
                                <p:ajax update="ajaxspinnervalue" process="@this" />
                            </p:spinner>

这是我要添加到列markSectionTwo

保存按钮

  <p:commandButton action="#{editMarkingBean.markSectionTwo}" value="#{bundle.buttonSave}" update=":growl" icon="ui-icon-disk"/>

支持豆:

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();
    }
}

和漫画标记实体

/ * *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具| 模板*,然后在编辑器中打开模板。 * /

package sws.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author Richard
 */
@Entity(name = "MARKING")
public class Marking implements Serializable {

    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;

    public String getStudentNumber() {
        return studentNumber;
    }

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

    public String getMarkingStage() {
        return markingStage;
    }

    public void setMarkingStage(String markingStage) {
        this.markingStage = markingStage;
    }

    public String getMarkingCompleted() {
        return markingCompleted;
    }

    public void setMarkingCompleted(String markingCompleted) {
        this.markingCompleted = markingCompleted;
    }

    public String getMarkSectionOne() {
        return markSectionOne;
    }

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

    public String getMarkSectionTwo() {
        return markSectionTwo;
    }

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

    public String getMarkSectionThree() {
        return markSectionThree;
    }

    public void setMarkSectionThree(String markSectionThree) {
        this.markSectionThree = markSectionThree;
    }

    public String getMarkSectionFour() {
        return markSectionFour;
    }

    public void setMarkSectionFour(String markSectionFour) {
        this.markSectionFour = markSectionFour;
    }

    public String getMarkSectionFive() {
        return markSectionFive;
    }

    public void setMarkSectionFive(String markSectionFive) {
        this.markSectionFive = markSectionFive;
    }

    public String getOveralMark() {
        return overalMark;
    }

    public void setOveralMark(String overalMark) {
        this.overalMark = overalMark;
    }

    public String getPlagorism() {
        return plagorism;
    }

    public void setPlagorism(String plagorism) {
        this.plagorism = plagorism;
    }

    public String getFeedback() {
        return feedback;
    }

    public void setFeedback(String feedback) {
        this.feedback = feedback;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Marking)) {
            return false;
        }
        Marking other = (Marking) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "sws.entities.Marking[ id=" + id + " ]";
    }

    public void setmarkSectionOne(String markSectionOne) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

编辑2:

我添加了一个postconstruct

@PostConstruct
public void markSectionTwo() {
    this.markingFacade.edit(this.markSectionTwo);
    markSectionTwo = new Marking();
    this.setMessage("Mark Saved");
}

但是现在我收到了错误消息http 500 error

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与表中已有的相同,则将markSectionTwo更新为该值而不是创建数据库中的新行

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}"

基本上说你必须有

  1. 称为editMarkingBean的托管Bean
  2. 托管bean中的一个名为markSectionTwo的对象,带有正确的getter和setter
  3. 对象markSectionTwo中的一个属性,称为markSectionTwo,具有正确的getter和setter

所以EL想要打电话的是

editMarkingBean.getMarkSectionTwo().getMarkSectionTwo()

请检查所有类,并在可能的情况下发布问题中的所有相关部分,例如类名(全部),托管bean作用域注释,getter,setter和属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM