2017-10-22 7 views
2

jspとサーブレットを使ってhibernate 4.3.xを使って簡単なアプリケーションを開発しようとしています。 jspを使用して作成されたテーブルにデータベースからテーブルのデータをロードします。ここでselectクエリの結果がcreateQuery()で返されない

は私role.jspファイルに必要なセクションが

<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> 

です。ここ

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; 
    } 
} 

Roleオブジェクトを表すためのRole.javaと呼ばれる私のPOJOクラスは、ここで必要があるさ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:スキーマの更新を完全 情報:休止状態:役割のリスト:rid1_0_としてrole0_.rid選択し、ROLEからrtitle2_0_としてrole0_.rtitleは 情報をrole0_ここで0

は私のダービーですデータベース階層

enter image description here

私はこのような環境に非常に新しいですし、多くの方法を試してみました。これを解決するために私を助けてください。あなたはそれを実装していなかったので、あなたがこのようlistCategories

を返す必要がnullのlistを返す

+0

は、テーブルには、データを持っていますか? – Juan

+0

はい、テーブルにデータがあります。 – Punya

+0

正しいデータベースに接続していますか(hibernate.cfg内) – Juan

答えて

1

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; 
    } 
関連する問題