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

标签 java sql jsf jpa

我想知道你是否可以帮助我

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

这是我的代码 atm,但我只是收到一个空点错误,并且不知道我做得是否正确

这是用户界面

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

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

和complte标记实体

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

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
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 以及属性。

关于java - 更新数据库中的记录 - JSF JPA 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920337/

相关文章:

java - 如何在java中的where子句中传递变量的值?

java - 连接后在 SQL 中打印新行

jsf - c :when and c:if don't work

java - 机器人:强制关闭

java - 将气泡排序从增加更改为减少

java - 如何在Java中运行C代码

mysql - SQL:获取哪一天下载的最大文件数

java - session 超时的工作方式与我希望的不同

jsf - 如何以编程方式配置 dataTable 的 rowSelect 事件监听器

java - 使用 Netty 的 HTTP 请求