我目前正在使用Hibernate Framework项目在Netbeans IDE上工作,以从数据库中检索值并将其显示在表结构中。我已经使用数据库表和控制器方法创建了映射来检索值。
我是hibernate框架的新手,我不太清楚如何在RoleController.java
视图中显示从Role.jsp
传递的角色列表。任何有关这方面的建议都将受到高度赞赏。
RoleController.java
package management;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
/**
*
* @author Taro
*/
@Named(value = "roleController")
@ManagedBean
@SessionScoped
public class RoleController implements Serializable {
int employeeId;
DataModel roleTitles;
String roleTitle;
RoleHelper helper;
public RoleController() {
helper = new RoleHelper();
}
public DataModel getRoleTitles() {
if (roleTitles == null) {
System.out.println("Successful");
roleTitles = new ListDataModel(helper.getRoleTitles());
}
return roleTitles;
}
}
Roles.jsp
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<div class="card">
<div class="header">
<h4 class="title">Available Roles</h4>
<p class="category">A list of Role Titles</p>
</div>
<div class="content">
<!--Display table of roles with an edit button against each role-->
</div>
</div>
</div>
...
RoleHelper.java
public class RoleHelper {
Session session = null;
public RoleHelper() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
/*
* Method : getRoleTitles
* @description Retrieve all the unique role titles
*/
public List getRoleTitles() {
List<Role> roleList = null;
try {
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery ("select distinct role.title from Role as role");
roleList = (List<Role>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return roleList;
}
...
Role.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 21, 2017 1:12:44 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="management.Role" table="ROLE" schema="APP" optimistic-lock="version">
<id name="roleid" type="int">
<column name="ROLEID" />
<generator class="assigned" />
</id>
<property name="employeeid" type="java.lang.Integer">
<column name="EMPLOYEEID" />
</property>
<property name="title" type="string">
<column name="TITLE" length="40" />
</property>
</class>
</hibernate-mapping>
Role.java
public class Role implements java.io.Serializable {
private int roleid;
private Integer employeeid;
private String title;
public Role() {
}
public Role(int roleid) {
this.roleid = roleid;
}
public Role(int roleid, Integer employeeid, String title) {
this.roleid = roleid;
this.employeeid = employeeid;
this.title = title;
}
public int getRoleid() {
return this.roleid;
}
public void setRoleid(int roleid) {
this.roleid = roleid;
}
public Integer getEmployeeid() {
return this.employeeid;
}
public void setEmployeeid(Integer employeeid) {
this.employeeid = employeeid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
}
答案 0 :(得分:0)
看起来您正在项目中使用JSF,因此我建议使用此框架的功能来构建UI。因此,您可以使用以下代码创建facelet index.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>JSF Example</title>
</h:head>
<h:body>
<h:dataTable value="#{roleController.getRoleTitles()}" var="roles">
<h:column>
<f:facet name="header">id</f:facet>
#{roles.roleid}
</h:column>
<h:column>
<f:facet name="header">Title</f:facet>
#{roles.title}
</h:column>
<h:column>
<h:form>
<h:commandButton value="Delete" action="#{roleController.delete(roles.roleid)}"/>
</h:form>
</h:column>
</h:dataTable>
</h:body>
</html>
此类代码的结果将如下。
我还重新设计了RoleController.java
,以便它可以支持删除操作:
import com.mberazouski.stackoverflow.components.utils.RoleHelper;
import com.mberazouski.stackoverflow.domain.Role;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.List;
@ManagedBean
@ViewScoped
public class RoleController implements Serializable {
private static final long serialVersionUID = 9160307746480440676L;
private RoleHelper helper;
public RoleController() {
helper = new RoleHelper();
}
public List<Role> getRoleTitles() {
return helper.getRoleTitles();
}
public void delete(int id) {
helper.delete(id);
}
}
此外,RoleHelper.java
已重新设计,因为它现在返回Role
个对象的集合。
import com.mberazouski.stackoverflow.domain.Role;
import com.mberazouski.stackoverflow.persistence.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
public class RoleHelper {
public List<Role> getRoleTitles() {
List<Role> roleList = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
try {
Query q = session.createQuery("FROM Role");
roleList = (List<Role>) q.list();
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
return roleList;
}
public void delete(int roleid) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
try {
Query q = session.createQuery("FROM Role role WHERE role.roleid = " + roleid);
Role role = (Role) q.list().get(0);
session.delete(role);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
}
另外,我有一些关于你Role.hbm.xml
的线索,我使用increment
生成器而不是assigned
,但我不确定你使用了哪个数据库,所以可能{{1}您的数据库服务器不支持。
希望这些信息足以继续您的工作。