2

我正在使用 jsf hibernate spring primefaces 的集成。我想更新一条记录。但我的代码不起作用。插入和删除效果很好。我没有错误。请帮我写正确的代码。个人道.java

public class PersonelDao implements IPersonelDao {
private SessionFactory sessionFactory;


public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
public void updatePersonel(Personel personel) {

    getSessionFactory().getCurrentSession().update(personel);
    }
public void deletePersonel(Personel personel) {
    getSessionFactory().getCurrentSession().delete(personel);

}

个人服务.java

@Transactional(readOnly=true)
public class PersonelService implements IPersonelService{
    IPersonelDao personeldao;
    public IPersonelDao getPersoneldao() {
        return personeldao;
    }

    public void setPersoneldao(IPersonelDao personeldao) {
        this.personeldao = personeldao;
    }

    @Transactional(readOnly=true)
    public void updatePersonel(Personel personel){
    getPersoneldao().updatePersonel(personel);
}
    @Transactional(readOnly=false)

    public void deletePersonel(Personel personel) {
        getPersoneldao().deletePersonel(personel);

    }

PersonelBean.java

@ManagedBean(name="personelMB")
@RequestScoped
public class PersonelBean implements Serializable{
    private static final long serialVersionUID = 1L;

    @ManagedProperty(value="#{PersonelService}")
    IPersonelService personelservice;
    List<Personel> personelList;

    private int personel_id;
    private String pname;
    private String pfamily;
    private String paddress;
    private String pphone;
//getter and setter
public void deletePersonel(int personel_id){
        Personel personel=(Personel)personelservice.getPersonelId(personel_id);
        getPersonelservice().deletePersonel(personel);


    }

    public void updatePersonel(RowEditEvent event){
    Personel personel = (Personel)event.getObject();
    getPersonelservice().updatePersonel(personel);
        //getDataFromDatabase();
    }

}

个人资料.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 dir="rtl" 
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:fn="http://java.sun.com/jsp/jstl/functions"
>
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>اطلاعات پرسنلی</title>
</h:head>
<h:body>
<h1>اضافه کردن پرسنل جدید</h1>
        <h:form>

            <h:panelGrid columns="4" >
              شماره پرسنلی : 
                <h:inputText id="id" value="#{personelMB.personel_id}" 
                    size="20" required="true"
                    label="id" >
                </h:inputText>
                <br></br>
                نام : 
                <h:inputText id="name" value="#{personelMB.pname}" 
                    size="20" required="true"
                    label="Name" >
                </h:inputText>

                 نام خانوادگی: 
                <h:inputText id="family" value="#{personelMB.pfamily}" 
                    size="20" required="true"
                    label="family" >
                </h:inputText> 

                آدرس : 
                <h:inputTextarea id="address" value="#{personelMB.paddress}" 
                    cols="30" rows="10" required="true"
                    label="Address" >
                </h:inputTextarea>


          تلفن:  
                <h:inputText id="tel" value="#{personelMB.pphone}" 
                    size="20" required="true"
                    label="tel" >
                </h:inputText> 
            </h:panelGrid>

            <h:commandButton value="درج اطلاعات" action="#{personelMB.addPersonel()}" />

        </h:form>
 <h2>مشاهده اطلاعات پرسنل</h2>  
 <h:form  prependId="false">  

    <p:dataTable id="pdataTable" var="personel" value="#{personelMB.personelList}" rowKey="#{personelMB.personel_id}" editable="true"
    >  
  <p:ajax event="rowEdit" update="@form" listener="#{personelMB.updatePersonel}"/>
        <f:facet name="header">  
            اطلاعات پرسنل 
        </f:facet>  

        <p:column> 

            <f:facet name="header">  
               شماره پرسنلی  
            </f:facet>  

            <h:outputText value="#{personel.personel_id}" />  

            <f:facet name="footer">  
                کدملی 
            </f:facet>  
        </p:column>  

        <p:column headerText="نام">
        <p:cellEditor>
        <f:facet name="output">  
            <h:outputText value="#{personel.pname}" />
            </f:facet>
            <f:facet name="input">
              <p:inputText value="#{personel.pname}" />
              </f:facet>
            </p:cellEditor> 
        </p:column>  

        <p:column headerText="نام خانوادگی"> 
         <p:cellEditor> 
         <f:facet name="output">
            <h:outputText value="#{personel.pfamily}" /> 
            </f:facet>
            <f:facet name="input">
            <p:inputText value="#{personel.pfamily}" />
            </f:facet> 
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="آدرس"> 
         <p:cellEditor> 
         <f:facet name="output">
            <h:outputText value="#{personel.paddress}" />  
            </f:facet>
            <f:facet name="input">
            <p:inputText value="#{personel.paddress}" />
            </f:facet>
            </p:cellEditor> 
        </p:column>  
   <p:column headerText="تلفن"> 
   <p:cellEditor> 
   <f:facet name="output">
            <h:outputText value="#{personel.pphone}" />
            </f:facet> 
            <f:facet name="input">
            <p:inputText value="#{personel.pphone}" /> 
            </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="حذف">
      <f:facet name="حذف">
         <h:outputText value="" />
      </f:facet>
      <p:commandButton icon="ui-icon-trash" title="حذف"
                       actionListener="#{personelMB.deletePersonel(personel.personel_id)}"  update="@form" ajax="true"/>
   </p:column>
   <p:column headerText="ويرايش">

         <p:rowEditor /> 



   </p:column>
        <f:facet name="footer">  
            تعداد رکورد#{fn:length(personelMB.getPersonelList())} میباشد.  
        </f:facet>  

    </p:dataTable>  

</h:form>  

</h:body>
</html>

个人资料.java

@Entity
@Table(name="TBLPERSONEL")
public class Personel {
@Id
@Column(name="PERSONEL_ID")
private int personel_id;
@Column(name="PNAME")
private String pname;
@Column(name="PFAMILY")
private String pfamily;
@Column(name="PADDRESS")
private String paddress;
@Column(name="PPHONE")
private String pphone;
@OneToOne
@PrimaryKeyJoinColumn
private Manager manager;
@OneToMany(mappedBy="personel")
private Set<Stufftransfer> stufftransfers;
public Personel(){

}
public Set<Stufftransfer> getStufftransfers() {
    return stufftransfers;
}
public void setStufftransfers(Set<Stufftransfer> stufftransfers) {
    this.stufftransfers = stufftransfers;
}
public Personel(int personel_id, String pname, String pfamily, String paddress,
        String pphone) {
    super();
    this.personel_id = personel_id;
    this.pname = pname;
    this.pfamily = pfamily;
    this.paddress = paddress;
    this.pphone = pphone;
}
//getter and setter
4

2 回答 2

0
  1. 使用p:commandButton而不是h:commandButton
  2. 设置更新属性p:commandButton

请记住,在更新属性中找到正确的 id 可能有点棘手


抱歉,我误解了这个问题正确的答案是:将您的更新方法更改为:

public void updatePersonel(RowEditEvent event){
Personel updated = (Pesonel)event.getObject();
//this is the updated value from the dataTable, get the entity from db and do the update

}

还有一件事:不要更新 ajax 事件上的行,因为您没有更新personelListupdate 方法中的 。这意味着rowEditajax 事件必须是这样的:

<p:ajax event="rowEdit" listener="#{personelMB.updatePersonel}"/>
于 2012-08-13T04:07:55.857 回答
0

像这样更改 ManagedBean 的 updatePersonel() 方法

    public void updatePersonel(int personel_id,String pname,String pfamily,String paddress,String pphone){
    Personel personel = new Personel();
    personel=(Personel)personelservice.getPersonelId(personel_id);
    if(personal != null) {
       personel.setPaddress(getPaddress());
       personel.setPersonel_id(getPersonel_id());
       personel.setPfamily(getPfamily());
       personel.setPname(getPname());
       personel.setPphone(getPphone());
       getPersonelservice().updatePersonel(personel_id, pname, pfamily, paddress, pphone);
    }
}
于 2012-08-13T08:33:09.953 回答