【问题标题】:createQuery() does not give the result for select querycreateQuery() 没有给出选择查询的结果
【发布时间】:2018-04-02 20:13:51
【问题描述】:

我将使用 jsp 和 servlet 使用 hibernate 4.3.x 开发一个简单的应用程序。我想将表数据从数据库加载到使用 jsp 创建的表中。

这是我的 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>

这是我的 pojo 类,名为 Role.java,用于表示角色对象

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

这是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:架构更新完成 信息:休眠:从 ROLE role0_ 中选择 role0_.rid 作为 rid1_0_,role0_.rtitle 作为 rtitle2_0_ 信息:角色列表:0

这是我的 derby 数据库层次结构

我对这个环境很陌生,尝试了很多方法。请帮我解决这个问题。

【问题讨论】:

  • 表有数据吗?
  • 是的,表有数据。
  • 您是否连接到正确的数据库(在 hibernate.cfg 中)
  • @Juan 是的。我连接到正确的数据库。这里我添加了 hibernate.cfg.xml 文件的代码

标签: hibernate jsp servlets derby glassfish-4


【解决方案1】:

你返回 list 这是 null 因为你没有实现它你需要返回 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2018-10-17
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多