【问题标题】:create update using Hibernate and Primefaces使用 Hibernate 和 Primefaces 创建更新
【发布时间】:2014-01-22 13:13:33
【问题描述】:

我有这个代码的 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

我认为我的逻辑是正确的,当我在另一个页面上尝试时,我也没有任何其他错误。

【问题讨论】:

  • 实体Users的标识是userId还是fullName?

标签: hibernate jsf


【解决方案1】:

这是因为,可能是您尝试将用户保存在 DB 中,而没有设置它的 fullName 属性,而您在 Users 类中设置了 fullNamae not nullable。

【讨论】:

    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2011-02-07
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多