2014-02-20 40 views
0

我想知道如果你能幫助我更新記錄在DB - JSF JPA等

基本上我創建了一個數據庫,並把它添加數據,數據表中的兩塊,留下的剩餘部分列空白,我想要做什麼,是能夠更新這些記錄與空白列的一些更多的數據,我怎麼能做到這一點?

這是我的代碼自動取款機,但我只是得到一個空點錯誤,不知道是否即時通訊做是正確的

這是U.I.

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

和comeplte標記實體

/* *要更改此許可證標題,請在項目屬性中選擇許可證標題。 *要更改此模板文件,請選擇工具|模板 *並在編輯器中打開模板。 */

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錯誤

javax.servlet.ServletException: WELD-000049 Unable to invoke public void sws.control.EditMarkingController.markSectionTwo() on [email protected] 
root cause 

org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke public void sws.control.EditMarkingController.markSectionTwo() on [email protected] 
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, "")); 
    } 
+0

你期望用marksectiontwo.marksectiontwo得到什麼? – Leo

+0

來更新db表中名爲marksectiontwo的列,它調用支持bean marksectiontwo中的類來做編輯,我應該真的把這些名稱改爲不一樣的東西 – user2061913

+0

但是bean中的對象叫做marktoedit – Leo

回答

1

您的錯誤信息創建一個數據庫中的新行

javax.el.PropertyNotFoundException (...) #{editMarkingBean.markSectionTwo.markSectionTwo}" 

基本上是說,你必須有

  1. 稱爲editMarkingBean
  2. 託管bean在你管理的bean對象稱爲markSectionTwo適當的getter和setter
  3. 在對象的屬性markSectionTwo稱爲markSectionTwo適當的getter和setter

有啥EL試圖調用是

editMarkingBean.getMarkSectionTwo().getMarkSectionTwo() 

請檢查所有的類,如果可能的話,在你的問題發佈的所有相關部分,如類名(全部),託管bean範圍註釋,獲取者和設置者以及屬性。

+0

謝謝,我已經檢查了一切,一切都在那裏,我有所有相關的getter和setter,並已更新以上全部代碼 – user2061913

+1

所以好多了,謝謝!你不需要初始化你的editMarkingBean.markSectionTwo嗎?在你的bean中添加一個用@PostConstruct註解的方法,並在那裏初始化它:-) – Leo

+0

嗨你有沒有關於如何做到這一點的例子,即時通訊有點困惑什麼是 – user2061913