Hibernate从数据库中获取列表 [英] Hibernate get List from database

查看:196
本文介绍了Hibernate从数据库中获取列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图获取包含数据库中所有产品的产品列表:

In the following code I am trying to get a List of Products which contains all the products in the database:

public List<Products> getAllProducts() throws Exception{
    try{
     List<Products> products ;
    org.hibernate.Transaction tx = session.beginTransaction();
    products = session.createSQLQuery("SELECT * FROM Products").list();
    if(products.size() > 0)
    {
        return products;
    }
    return null;  
    }
    catch(Exception e)
    {
        throw e;
    }
}

但是会抛出此异常:

[Ljava.lang.Object; cannot be cast to mediatek.Products 

推荐答案

List<Products> list = session.createCriteria(Products.class).list();

这将为您提供数据库中产品表的所有记录

This will give you all the records of products table from database

这篇关于Hibernate从数据库中获取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆