使用Hibernate和Primefaces创建更新

时间:2014-01-22 13:13:34

标签: hibernate jsf

我有一个使用此代码的primefaces视图,

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" /> 
<p:panelGrid columns="3" cellpadding="5">
    <h:outputLabel value = "Username" />
    <h:inputText value="#{usersController.users.userId}" id="username"/>
    <h:messages for="username" />

    <h:outputLabel value = "Pertanyaan Rahasia" />
    <p:inputText id="secretQuestion" value="#{usersController.users.secretQuestion}"/>
    <h:messages for="secretQuestion" />

    <h:outputLabel value = "Jawaban Rahasia" />
    <p:inputText id="secretAnswer" value="#{usersController.users.secretAnswer}"/>
    <h:messages for="secretAnswer"/>



    <h:outputLabel value = "Password Lama" />
    <p:password  id="oldPassword" value="#{usersController.users.password}" required="true" requiredMessage="Please Enter Old Password" >
        <f:validator validatorId="com.piwi.controller.usercontroller.passwordController" />
    </p:password>    
    <h:message for="oldPassword" style="color:red" />


    <h:outputLabel value = "Password Baru" for="newPassword"/>
    <p:inputText id="newPassword"  value="#{usersController.users.password}" required="true"/>
    <h:messages for="newPassword" />

    <h:outputLabel value = "Konfirmasi Password" for="confirmPassword"/>
    <p:inputText id="confirmPassword" value="#{usersController.users.password}" required="true" match="newPassword"/>
    <h:messages for="confirmPassword" />


    <p:commandButton value="Submit" id="myButton" action="#{usersController.updateUserPassword}"/><p:commandButton type="reset" value="Cancel"/>
</p:panelGrid> 

这是我的逻辑:

public String updateUserPassword(){
    try{
        LOG.info("Updating Password : "+sessionBean.getUsername());
        String userId = (String) FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();           
        userBusinessLogic.doUpdateUser(users);
    }catch(Exception e){
        LOG.error("AdminController.updateUser().", e);
        //e.printStackTrace();  
    }
    //prepareList();
    users = new Users();
    FacesMessage message = new FacesMessage("Update Sukses");
    return "create_user.xhtml?faces-redirect=true";

}
public String doUpdateUser(Users users){
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try{
        trns = session.beginTransaction();
        session.update(users);
        session.getTransaction().commit();
    }catch(Exception e){
        LOG.error("UserBusinessLogic.doUpdateUser()", e);
        e.printStackTrace();
        if(trns != null){
            trns.rollback();
        }
    }finally{
        session.flush();
        session.close();    
    }
    return null;    
}

我想问一下,当我点击更新按钮时,我总是会出现错误:

2014-01-22 19:59:01,396 ERROR - UserBusinessLogic.doUpdateUser()

org.hibernate.PropertyValueException:not-null属性引用null或tra nsient值:com.piwi.dbobject.mlm.Users.fullName

我认为我的逻辑是真的,当我用另一页尝试时,我也没有任何其他错误。

1 个答案:

答案 0 :(得分:0)

这是因为,您可能尝试在DB中保存用户,而不设置fullName属性,您在fullNamae类中将Users设置为不可为空。