PrimeFaces + DataTable(编辑行)+ Hibernate + MYSQLDatabase [英] PrimeFaces + DataTable(edit row) + Hibernate+ MYSQLDatabase
问题描述
我想编辑datatable行。我的代码在这里
I would like to edit datatable row. My code here
我的ManagedBean
my ManagedBean
@ManagedBean
@ViewScoped
public class course implements Serializable{
private int course_Id ;
private String course_Name;
private Integer course_Hours ;
private Date course_Date;
private Double course_Prise ;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
private String sd ;
getter and setter .....
public void onRowEdit(RowEditEvent event) {
course c =((course) event.getObject());
Transaction trn = null ;
Session session = HibernateUtil.getSessionFactory().openSession();
trn = session.beginTransaction();
c.setCourse_Id(getCourse_Id());
c.setCourse_Name(getCourse_Name());
c.setCourse_Hours(getCourse_Hours());
c.setCourse_Prise(getCourse_Prise());
c.setCourse_Date(getCourse_Date());
session.update(c);
session.getTransaction().commit();
FacesMessage msg = new FacesMessage("Course Edited", ((course) event.getObject()).getCourse_Name());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
我的JSF页面
<?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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form id="form">
<p:growl id="msgs" showDetail="true"/>
<p:dataTable id="dt" value="#{course.allcourses}" var="course" editable="true">
<p:ajax event="rowEdit" listener="#{course.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{course.onRowCancel}" update=":form:msgs" />
<p:column id="colId" filterBy="#{course.course_Id}" sortBy="#{course.course_Id}"
headerText="ID" footerText="contains"
filterMatchMode="contains">
<h:outputText value="#{course.course_Id}" />
</p:column>
<p:column filterBy="#{course.course_Name}" headerText="Name" sortBy="#{course.course_Name}">
<p:cellEditor >
<f:facet name="output">
<h:outputText value="#{course.course_Name}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{course.course_Name}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column filterBy="#{course.course_Hours}" headerText="Hours" sortBy="#{course.course_Hours}">
<p:cellEditor >
<f:facet name="output">
<h:outputText value="#{course.course_Hours}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{course.course_Hours}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column filterBy="#{course.course_Prise}" headerText="Prise" sortBy="#{course.course_Prise}">
<p:cellEditor >
<f:facet name="output">
<h:outputText value="#{course.course_Prise}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{course.course_Prise}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column filterBy="#{course.course_Date}" headerText="Start Date" sortBy="# {course.course_Date}">
<p:cellEditor >
<f:facet name="output">
<h:outputText value="#{course.course_Date}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{course.sd}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</h:body>
当我修改单元格我点击更新按钮
然后单元格返回到原始状态
When I modify the cells and i click update button, then the cells return to original state
我如何修改和更新行?
非常感谢你
注意
当我使用这个代码 c.setCourse_Name (Hibernate);
例如更新工作很好
这是我的方法,当然Bean
this is my method in course Bean
`public List <course> getAllcourses(){
List<course> courses ;
courseDAO dao2 = new courseDAO();
courses = dao2.getCourses();
return courses;
}
`
这是getCourses Mathod DAo类
and this is getCourses Mathod in course DAo Class
`public List<course> getCourses (){
List<course> allcourses = null ;
Transaction trns = null;
Session session =HibernateUtil.getSessionFactory().openSession();
try {
trns= session.beginTransaction();
allcourses = session.createCriteria(course.class).list();
session.getTransaction().commit();
} catch (HibernateException e) {
e.getMessage();
trns.rollback();
} finally {
session.flush();
session.close();
}
return allcourses;
}
`
**您在代码中看到任何错误? *
` ** you see any errors in my code ?**
推荐答案
你的逻辑在这里转过来...你从事件中获得你的课程对象:
Your logic is turned around here... You are getting your course object from the event:
course c =((course) event.getObject());
其中包含来自datatable的更新值,然后将值替换为旧的值,方法是调用
which holds the updated values from the datatable, then you replace the values with the old ones by calling the setters.
删除所有这些,它应该工作:
Remove all this and it should work:
c.setCourse_Id(getCourse_Id());
c.setCourse_Name(getCourse_Name());
c.setCourse_Hours(getCourse_Hours());
c.setCourse_Prise(getCourse_Prise());
c.setCourse_Date(getCourse_Date());
这篇关于PrimeFaces + DataTable(编辑行)+ Hibernate + MYSQLDatabase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!