数据库记录没有更新但是java代码说1行受影响(Primefaces)

时间:2016-11-18 12:15:26

标签: java java-ee primefaces jsf-2

我是J2EE的新用户,我正在尝试执行Crud次操作。但我被困在update method

我对EJB使用server (database)jsf-primefaces使用client side

问题在于,每当我点击edit ajax event时,它都会将我的行转到input text这是正确的,之后当我编辑字段时,我按ok更新行,但确实如此。

这就是我到目前为止所做的!!!

xhtml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>JSF 2.x Page</title>
    <meta http-equiv="keywords" content="enter,your,keywords,here" />
    <meta http-equiv="description"
        content="A short description of this page." />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</h:head>
<h:body>
<h:form id="form">
   <p:growl id="msgs" showDetail="true"/>

   <p:dataTable id="rec1" var="item"  value="#{first.list}" paginator="true" editable="true" style="width:550px;" >
       <f:facet name="header">
           Personal Record
       </f:facet>

       <p:ajax event="rowEdit" listener="#{first.onRowEdit}" update=":form:msgs" />
       <p:ajax event="rowEditCancel" listener="#{first.onRowCancel}" update=":form:msgs" />


       <p:column headerText="Id">
           <p:cellEditor>
               <f:facet name="output"><h:outputText value="#{item.id}" /></f:facet>
               <f:facet name="input"><p:inputText id="modelInput" value="#{item.id}" style="width:100%"/></f:facet>
           </p:cellEditor>
       </p:column>

       <p:column headerText="First Name">
           <p:cellEditor>
               <f:facet name="output"><h:outputText value="#{item.fname}" /></f:facet>
               <f:facet name="input"><p:inputText value="#{item.fname}" style="width:100%" label="First Names"/></f:facet>
           </p:cellEditor>
       </p:column>

       <p:column headerText="Last Name">
           <p:cellEditor>
               <f:facet name="output"><h:outputText value="#{item.lname}" /></f:facet>
               <f:facet name="input"><p:inputText value="#{item.lname}" style="width:100%"/></f:facet>
           </p:cellEditor>
       </p:column>


       <p:column headerText="Delete">

           <p:commandButton icon="ui-icon-close" title="Delete"
                           actionListener="#{first.removeUser(item)}" update="rec1" />
      </p:column>

       <p:column style="width:32px">
           <p:rowEditor />
       </p:column>


  </p:dataTable>

   <br/>

   <br/>
   <br/>

   <p id="hello">
First Name:
            <p:inputText value="#{first.fname}"></p:inputText>
            <br /> Last Name:
             <p:inputText value="#{first.lname}"></p:inputText>
            <br /> 
    <p:growl id="growl" life="5000" />
   <p:commandButton value="Insert" update="rec1" action="#{first.add}"></p:commandButton>

    <br /><h:messages id="test"></h:messages>
</p>






</h:form>
</h:body>
</html>

这是我的ManagedBean

package jsfpckg;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
//import javax.faces.bean.ViewScoped;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;



import org.primefaces.event.RowEditEvent;

import model.User;

import jsfejb.sbeanRemote;

@ManagedBean(name = "first")
@SessionScoped
public class First {



    @EJB(mappedName = "ejb:/ejb/sbean!jsfejb.sbeanRemote")
    sbeanRemote data;

    private List<User> list;
    private String fname;
    private int id;

    public int getId() {
        return id;
    }

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

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    private String lname;

    public void setList(List<User> list) {
        this.list = list;
    }


//    public void onRowEdit(RowEditEvent event) {
//        FacesMessage msg = new FacesMessage("Record Edited", ((user) event.getObject()).getId());
//        FacesContext.getCurrentInstance().addMessage(null, msg);
//    }
//     
//    public void onRowCancel(RowEditEvent event) {
//        FacesMessage msg = new FacesMessage("Edit Cancelled", ((user) event.getObject()).getId());
//        FacesContext.getCurrentInstance().addMessage(null, msg);
//    }

    public List<User> getList() {

        list = data.retriveDatatypes();
     //   System.out.println("list in first class is" + list.get(1));
        return list;

    }


    public String add() {

        data.insertUser(fname, lname);
        FacesContext.getCurrentInstance().addMessage(" Succesfully added to database ",
                new javax.faces.application.FacesMessage("Succesfully added to database"));
         return "Null";
    }

        public void removeUser(User user){

            System.out.println("deleted");
        int id= user.getId();
        //int user1= ((User) event.getObject()).getId();
        data.deleteUser(id);


    }



    public void onRowEdit(RowEditEvent event) {


        int user1= ((User) event.getObject()).getId();
        String fname1= ((User) event.getObject()).getFname();
        String lname1= ((User) event.getObject()).getLname();


        //list.remove(list.get(user1));
        data.saveUser(user1, fname1, lname1);
        System.out.println("name., id, is "+user1+fname1+lname1);
        FacesMessage msg = new FacesMessage("Record Edited for ID: "+user1);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }


    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Edit Cancelled");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }



}

这是我的EJB项目bean文件,我写了所有查询

public void saveUser(int id, String fname, String lname) {
        System.out.println("recieveing records: "+id+fname+lname);
        String query = "UPDATE user SET fname='"+fname+"', lname='"+lname+"' where id= "+id;

// int outcome = em.createNativeQuery(query).executeUpdate();

//      User user= em.find(User.class, id);
//      user.setFname(fname);
//      user.setLname(lname);
//      em.persist(user);
//      em.merge(user);

        System.out.println("outcome"+ outcome);
    }
  

注意:这件事适用于update,但我不想使用此

            User user= em.find(User.class, id);
            user.setFname(fname);
            user.setLname(lname);
            em.persist(user);
            em.merge(user);

this is the main table appears after clicking edit

says record edited!!! but didnt changed the database values. the query result also shows 1 which means row 1 row is affected

0 个答案:

没有答案