我想编辑我的数据表并更新mysql数据库。我正在使用primefaces我编辑数据表,但它不起作用。当我把这个:
FacesMessage msg = new FacesMessage("Annee Edited",""+((Annee) event.getObject()).getAnnee());
FacesContext.getCurrentInstance().addMessage(null, msg);
数据表中的值发生变化,我收到消息Annee Edited。但是当我添加:
Annee anneee = anneeMetier.getAnnee(((Annee) event.getObject()).getAnnee());
anneeMetier.update(anneee);
它不起作用,它不会在数据表上也不会在数据库中发生变化。 我正在使用jsf,spring和hibernate。如果有人可以帮忙解决这个问题,我会很感激。
Annee.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:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
</h:head>
<h:body>
<h:form id="form1">
<p:growl id="messages" showDetail="true"/>
<p:panel header="Sale Item" style="width: 400px;">
<p:spacer height="30px;"/>
<p:dataTable value="#{anneeBean.listeAnnees}" var="o" widgetVar="50" style="width: 60px;" editable="true">
<p:ajax event="rowEdit" listener="#{anneeBean.onEdit}" update=":form1:messages" />
<p:ajax event="rowEditCancel" listener="#{anneeBean.onCancel}" update=":form1:messages" />
<p:column>
<f:facet name="header">
<h:outputText value="Item Name" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{o.annee}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{o.annee}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options" style="width:50px">
<p:rowEditor />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
</h:body>
</html>
AnneeBean.java
package controller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import model.services.AnneeMetier;
import net.vo.Annee;
import org.primefaces.event.RowEditEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("view")
public class AnneeBean implements Serializable {
private static final long serialVersionUID = 1L;
private int annee;
@Autowired
private AnneeMetier anneeMetier;
private List<Annee> listeAnnees;
public AnneeBean() {
}
@PostConstruct
public void init() {
listeAnnees = anneeMetier.getAllAnnees();
}
public List<Annee> getListeAnnees() {
return listeAnnees;
}
private AnneeBean(int annee) {
this.annee=annee;
}
public int getAnnee() {
return annee;
}
public void setAnnee(int annee) {
this.annee = annee;
}
public void onEdit(RowEditEvent event) {
Annee anneee = anneeMetier.getAnnee(((Annee) event.getObject()).getAnnee());
anneeMetier.update(anneee);
FacesMessage msg = new FacesMessage("Item Edited",""+((Annee) event.getObject()).getAnnee());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Item Cancelled");
FacesContext.getCurrentInstance().addMessage(null, msg);
//orderList.remove((Annee) event.getObject());
}
}
这是我在商业和dao层中的更新方法: 业务
@Override
public void update(Annee annee) {
anneeDao.update(annee);
}
dao:
@Override
public void update(Annee annee) {
Session session = HibernateUtil.getSession();
Transaction tx=null;
try
{
tx=session.beginTransaction();
session.update(annee);
tx.commit();
}
catch(RuntimeException e)
{
if(tx != null)
{
tx.rollback();
}
throw e;
}
finally
{
session.close();
}
}