-1

我正在使用spring 3 hibernate 4 jsf和的集成primefaces。在我的数据表中我可以记录insertdelete但我的更新代码不起作用并且无法更新表和数据库。我不想使用 primefaces cell editor,我想要command button执行更新。但是当我单击命令按钮时,它会删除数据库和数据表中的单元格(字段),除了 personel_id 字段。请帮助我获得正确的代码。 在此处输入图像描述 当我编辑字段并单击更新按钮时: 在此处输入图像描述 personelDao.java

public class PersonelDao implements IPersonelDao {
    private SessionFactory sessionFactory;
    public void addPersonel(Personel personel) {
        getSessionFactory().getCurrentSession().save(personel);
    }
    public Personel updatePersonel(Personel personel) {

final Session session = getSessionFactory().getCurrentSession();
if(session.contains(personel)){
    session.update(personel);
} else {
    personel = (Personel) session.merge(personel);
}
return personel;
}
public void deletePersonel(Personel personel) {
            getSessionFactory().getCurrentSession().delete(personel);
        }
    }

个人服务.java

@Transactional(readOnly=true)
public class PersonelService implements IPersonelService{
    IPersonelDao personeldao;
    @Transactional(readOnly=false)
    public void addPersonel(Personel personel) {
        getPersoneldao().addPersonel(personel);
    }

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

个人豆类.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 addPersonel(){
            Personel personel=new Personel();
            personel.setPaddress(getPaddress());
            personel.setPersonel_id(getPersonel_id());
            personel.setPfamily(getPfamily());
            personel.setPname(getPname());
            personel.setPphone(getPphone());
            getPersonelservice().addPersonel(personel);
            reset();
    }

    public void deletePersonel(int personel_id){
            Personel personel=(Personel)personelservice.getPersonelId(personel_id);
            getPersonelservice().deletePersonel(personel);
    }

    public void update(int personel_id){
        Personel personel=(Personel)personelservice.getPersonelId(personel_id);
        personel.setPaddress(getPaddress());
        personel.setPfamily(getPfamily());
        personel.setPname(getPname());
        personel.setPphone(getPphone());
        getPersonelservice().updatePersonel(personel);
    }
}

个人资料.xhtml

    <h2>مشاهده اطلاعات پرسنل</h2>   
     <h:form  prependId="false">  
      <p:dataTable id="pdataTable" var="personel" value="#{personelMB.personelList}" rowKey="#{personelMB.personel_id}" editable="true"
         >  
            <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:inputText value="#{personel.pname}" size="5"/>
                 </p:column>  
      <p:column headerText="نام خانوادگی"> 
             <p:inputText value="#{personel.pfamily}" size="5"/>
                </p:column>  
      <p:column headerText="آدرس"> 
            <p:inputText value="#{personel.paddress}" size="5"/>
            </p:column>  
       <p:column headerText="تلفن"> 
                <p:inputText value="#{personel.pphone}" size="5"/> 
            </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:commandButton icon="ui-icon-pencil" title="ويرایش"
                           actionListener="#{personelMB.update(personel.personel_id)}"  update="@form" ajax="true"/>
</p:column>
        <f:facet name="footer">  
            تعداد رکورد#{fn:length(personelMB.getPersonelList())} میباشد.  
        </f:facet>  

    </p:dataTable> 

个人资料.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(){

    }
    //getter and setter
}

痕迹:

Hibernate: update TBLPERSONEL set PADDRESS=?, PFAMILY=?, PNAME=?, PPHONE=? where PERSONEL_ID=?
Hibernate: select personel0_.PERSONEL_ID as PERSONEL1_1_, personel0_.PADDRESS as PADDRESS1_, personel0_.PFAMILY as PFAMILY1_, personel0_.PNAME as PNAME1_, personel0_.PPHONE as PPHONE1_ from TBLPERSONEL personel0_
Hibernate: select manager0_.ID as ID0_1_, manager0_.PASSWORD as PASSWORD0_1_, manager0_.PERSONEL_ID as PERSONEL3_0_1_, personel1_.PERSONEL_ID as PERSONEL1_1_0_, personel1_.PADDRESS as PADDRESS1_0_, personel1_.PFAMILY as PFAMILY1_0_, personel1_.PNAME as PNAME1_0_, personel1_.PPHONE as PPHONE1_0_ from TBLMANAGER manager0_ left outer join TBLPERSONEL personel1_ on manager0_.ID=personel1_.PERSONEL_ID where manager0_.ID=?
Hibernate: select manager0_.ID as ID0_1_, manager0_.PASSWORD as PASSWORD0_1_, manager0_.PERSONEL_ID as PERSONEL3_0_1_, personel1_.PERSONEL_ID as PERSONEL1_1_0_, personel1_.PADDRESS as PADDRESS1_0_, personel1_.PFAMILY as PFAMILY1_0_, personel1_.PNAME as PNAME1_0_, personel1_.PPHONE as PPHONE1_0_ from TBLMANAGER manager0_ left outer join TBLPERSONEL personel1_ on manager0_.ID=personel1_.PERSONEL_ID where manager0_.ID=?
Hibernate: select manager0_.ID as ID0_1_, manager0_.PASSWORD as PASSWORD0_1_, manager0_.PERSONEL_ID as PERSONEL3_0_1_, personel1_.PERSONEL_ID as PERSONEL1_1_0_, personel1_.PADDRESS as PADDRESS1_0_, personel1_.PFAMILY as PFAMILY1_0_, personel1_.PNAME as PNAME1_0_, personel1_.PPHONE as PPHONE1_0_ from TBLMANAGER manager0_ left outer join TBLPERSONEL personel1_ on manager0_.ID=personel1_.PERSONEL_ID where manager0_.ID=?
Hibernate: select personel0_.PERSONEL_ID as PERSONEL1_1_, personel0_.PADDRESS as PADDRESS1_, personel0_.PFAMILY as PFAMILY1_, personel0_.PNAME as PNAME1_, personel0_.PPHONE as PPHONE1_ from TBLPERSONEL personel0_
Hibernate: select manager0_.ID as ID0_1_, manager0_.PASSWORD as PASSWORD0_1_, manager0_.PERSONEL_ID as PERSONEL3_0_1_, personel1_.PERSONEL_ID as PERSONEL1_1_0_, personel1_.PADDRESS as PADDRESS1_0_, personel1_.PFAMILY as PFAMILY1_0_, personel1_.PNAME as PNAME1_0_, personel1_.PPHONE as PPHONE1_0_ from TBLMANAGER manager0_ left outer join TBLPERSONEL personel1_ on manager0_.ID=personel1_.PERSONEL_ID where manager0_.ID=?
Hibernate: select manager0_.ID as ID0_1_, manager0_.PASSWORD as PASSWORD0_1_, manager0_.PERSONEL_ID as PERSONEL3_0_1_, personel1_.PERSONEL_ID as PERSONEL1_1_0_, personel1_.PADDRESS as PADDRESS1_0_, personel1_.PFAMILY as PFAMILY1_0_, personel1_.PNAME as PNAME1_0_, personel1_.PPHONE as PPHONE1_0_ from TBLMANAGER manager0_ left outer join TBLPERSONEL personel1_ on manager0_.ID=personel1_.PERSONEL_ID where manager0_.ID=?
Hibernate: select manager0_.ID as ID0_1_, manager0_.PASSWORD as PASSWORD0_1_, manager0_.PERSONEL_ID as PERSONEL3_0_1_, personel1_.PERSONEL_ID as PERSONEL1_1_0_, personel1_.PADDRESS as PADDRESS1_0_, personel1_.PFAMILY as PFAMILY1_0_, personel1_.PNAME as PNAME1_0_, personel1_.PPHONE as PPHONE1_0_ from TBLMANAGER manager0_ left outer join TBLPERSONEL personel1_ on manager0_.ID=personel1_.PERSONEL_ID where manager0_.ID=?
4

2 回答 2

0

为防止更新时出现会话丢失问题,dao 中的更改:

public class PersonelDao implements IPersonelDao {
    ...
    public Personel updatePersonel(Personel personel) {
        final Session session = getSessionFactory().getCurrentSession();
        if(session.contains(personel)){
            session.update(personel);
        } else {
            personel = (Personel) session.merge(personel);
        }
        return personel;
    }
    ...
}

因此,如果实体不在会话中,将被迫将其与合并链接。

您还必须检索合并返回的对象,您可能不得不强制并更改内存中的引用。

问候,

于 2012-08-26T15:13:03.487 回答
0

仅在方法中定义事务性并分配传播,如果您在类上定义“事务性”,则适用于所有方法。

personelservice.java 的更改:

//@Transactional(readOnly=true)
public class PersonelService implements IPersonelService{
    IPersonelDao personeldao;
    @Transactional(propagation=Propagation.REQUIRED)
    public void addPersonel(Personel personel) {
        getPersoneldao().addPersonel(personel);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public void updatePersonel(Personel personel){
        getPersoneldao().updatePersonel(personel);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public void deletePersonel(Personel personel) {
        getPersoneldao().deletePersonel(personel);
    }
}

问候,

马努·纳瓦罗。

PS:原谅我的英文小流水

于 2012-08-22T15:01:23.497 回答