2017-10-22 113 views
2

我將開發一個使用hibernate 4.3.x的簡單應用程序,使用jsp和servlet。我想從數據庫加載表格數據到使用jsp創建的表格。createQuery()不給出選擇查詢的結果

這裏是我的role.jsp爲代表角色對象

package com.hrmweb.model; 

import java.io.Serializable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import static javax.persistence.GenerationType.IDENTITY; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import org.hibernate.annotations.GenericGenerator; 

@Entity 
@Table(name="ROLE") 
public class Role implements Serializable{ 
    @Id 
    @GenericGenerator(name="gen",strategy="increment") 
    @GeneratedValue(generator="gen") 
    @Column(name = "rid", unique = true, nullable = false, precision = 15, scale = 0) 
    private long rid; 
    private String rtitle; 


    public Role() { 
    } 

    public Role(String rtitle) { 
     this.rtitle = rtitle; 
     System.out.println(rtitle); 
    } 

    public long getRid() { 
     return rid; 
    } 

    public void setRid(long rid) { 
     this.rid = rid; 
    } 

    public String getRtitle() { 
     return rtitle; 
    } 

    public void setRtitle(String rtitle) { 
     this.rtitle = rtitle; 
    } 
} 

這裏所需要的部分文件

<table class="table"> 
    <thead> 
     <tr> 
     <th>Role ID</th> 
     <th>Role Title</th> 
     <th>Actions</th> 
     </tr> 
    </thead> 
    <tbody> 
     <% 
        DBManager db = new DBManager(); 
        List<Role> list = db.getListOfRoles(); 
        for (Role r : list) { 
       %> 
     <tr> 
     <td><%=r.getRid()%></td> 
     <td><%=r.getRtitle()%></td> 
     <td><button type="button" class="btn btn-secondary" action="viewRoles.jsp">View</button> 
      <button type="button" class="btn btn-secondary" action="updateRoles.jsp">Update</button> 
     </td> 
     </tr> 
     <%}%> 
    </tbody> 
    </table> 

這裏是我的POJO類叫做Role.java是必需的部分用於數據庫事務處理DBManager.java

public List<Role> getListOfRoles(){ 
     List<Role> list = new ArrayList<Role>(); 
     Session session = HibernateUtil.openSession(); 
     Transaction tx = null;  
     try { 
      tx = session.getTransaction(); 
      tx.begin(); 
      Query query = session.createQuery("from Role"); 
      List<Role> listCategories = query.list(); 

      System.out.println("Roles List : "+list.size()); 
      tx.commit(); 
     } catch (Exception e) { 
      if (tx != null) { 
       tx.rollback(); 
      } 
      e.printStackTrace(); 
     } finally { 
      session.close(); 
     } 
     return list; 
    } 

的hibernate.cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property> 
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> 
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/test</property> 
    <property name="hibernate.connection.username">test</property> 
    <property name="hibernate.connection.password">test</property> 
    <property name="show_sql">true</property> 
    <property name="hbm2ddl.auto">update</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <mapping class="com.hrmweb.model.user"/> 
    <mapping class="com.hrmweb.model.Role"/> 
    </session-factory> 
</hibernate-configuration> 

但是當我執行我的應用程序不通過的createQuery()方法返回值的任何名單。 GlassFish服務器也不會提供任何錯誤消息。所以這裏是服務器輸出。

信息:HHH000232:架構更新完成 信息:休眠:選擇role0_.rid爲rid1_0_,role0_.rtitle從ROLE rtitle2_0_ role0_ 信息:角色列表:0

這裏是我的德比數據庫層次

enter image description here

我非常新的這個環境,試了很多辦法。請幫我解決這個問題。

+0

是否有表數據? – Juan

+0

是的,表格有數據。 – Punya

+0

您是否連接到正確的數據庫(在hibernate.cfg中) – Juan

回答

1

將返回list,因爲你沒有實現它,你需要返回listCategories

這樣是空:

public List<Role> getListOfRoles(){ 
     List<Role> list = new ArrayList<Role>(); 
     Session session = HibernateUtil.openSession(); 
     Transaction tx = null;  
     try { 
      tx = session.getTransaction(); 
      tx.begin(); 
      Query query = session.createQuery("from Role"); 
      List<Role> listCategories = query.list(); 

      System.out.println("Roles List : "+listCategories.size()); 
      tx.commit(); 
     } catch (Exception e) { 
      if (tx != null) { 
       tx.rollback(); 
      } 
      e.printStackTrace(); 
     } finally { 
      session.close(); 
     } 
     return listCategories; 
    }