我想对单元格编辑进行更新查询。我可以编辑现有的单元格但不能在数据库上更新它,我无法在单元格编辑上进行Hibernate查询。所以请帮助。
这是xhtml数据表
<h:form id="view">
<p:growl id="msgs" showDetail="true"/>
<p:panel header="View all" >
<p:dataTable var="viewall" value="#{UserController.info}" id="viewalldata" editable="true" resizableColumns="true" style="margin-bottom:20px">
<p:ajax event="rowEdit" listener="#{UserController.onRowEdit}" update=":view:msgs" />
<p:ajax event="rowEditCancel" listener="#{UserController.onRowCancel}" update=":view:msgs" />
<p:ajax event="cellEdit" listener="#{UserController.onCellEdit}" update=":view:msgs" />
<p:column headerText="Id">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.id}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{viewall.id}" style="width:100%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="UserName">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.userName}" /></f:facet>
<f:facet name="input"><p:inputText value="#{viewall.userName}" style="width:100%" label=""/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="FirstName">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.firstName}" /></f:facet>
<f:facet name="input"><p:inputText value="#{viewall.firstName}" style="width:100%" label=""/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="LastName">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.lastName}" /></f:facet>
<f:facet name="input"><p:inputText value="#{viewall.lastName}" style="width:100%" label=""/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Password">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.password}" /></f:facet>
<f:facet name="input"><p:inputText value="#{viewall.password}" style="width:100%" label=""/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Email">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.email}" /></f:facet>
<f:facet name="input"><p:inputText value="#{viewall.email}" style="width:100%" label=""/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Phone">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{viewall.phone}" /></f:facet>
<f:facet name="input"><p:inputText value="#{viewall.phone}" style="width:100%" label=""/></f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
输出是:[这是从MySQL数据库读取所有数据后的视图。] [1]
这是我的ManagedBean
@ManagedBean(name = "UserController")
@SessionScoped
public class UserController {
private User RegistrationUser;
public User getRegistrationUser() {
return RegistrationUser;
}
public void setRegistrationUser(User RegistrationUser) {
this.RegistrationUser = RegistrationUser;
}
public UserController() {
this.RegistrationUser = new User();
}
public String register() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(RegistrationUser);
session.getTransaction().commit();
session.close();
System.out.println("Register User :" + this.RegistrationUser);
String msg = "User Registered Successfully";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
return "registration";
}
public String login() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "From User where userName='" + RegistrationUser.getUserName() + "' and password='" + RegistrationUser.getPassword() + "'";
Query query = session.createQuery(hql);
if (!query.list().isEmpty()) {
System.out.println("success log in");
} else {
System.out.println("not succeed at log in!");
}
session.beginTransaction();
session.getTransaction().commit();
session.close();
return null;
}
public List<User> getInfo() {
return getUserInfo();
}
public List<User> getUserInfo() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from User";
Query q = sessionFactory.openSession().createQuery(hql);
List<User> list = q.list();
return list;
}
public void onRowEdit(RowEditEvent event) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "update User set userName=:userName,firstName=:firstName,lastName=:lastName,password=:password,email=:email,phone=:phone where id=:id";
Query query = session.createQuery(hql);
query.setParameter("userName", RegistrationUser.getUserName());
query.setParameter("firstName", RegistrationUser.getFirstName());
query.setParameter("lastName", RegistrationUser.getLastName());
query.setParameter("password", RegistrationUser.getPassword());
query.setParameter("email", RegistrationUser.getEmail());
query.setParameter("phone", RegistrationUser.getPhone());
query.setParameter("id", RegistrationUser.getId());
session.beginTransaction();
query.executeUpdate();
session.getTransaction().commit();
session.close();
FacesMessage msg = new FacesMessage("Cell Edited", ((User) event.getObject()).getId().toString());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Cell Edit Cancelled", ((User) event.getObject()).getId().toString());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue().toString();
Object newValue = event.getNewValue().toString();
if (newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
这是用户类
public class User implements java.io.Serializable {
private Integer id;
private String userName;
private String password;
private String firstName;
private String lastName;
private String email;
private String phone;
public User() {
}
public User(String userName, String password) {
this.userName = userName;
this.password = password;
}
public User(String userName, String password, String firstName, String lastName, String email, String phone) {
this.userName = userName;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
答案 0 :(得分:0)
在onRowEdit方法中,更改设置参数,如下所示 -
query.setParameter("userName", ((User) event.getObject()).getUserName());
query.setParameter("firstName", ((User) event.getObject()).getFirstName());
......等等