如何使用hibernate获取表中的所有数据?

时间:2017-03-27 04:11:22

标签: java spring hibernate jsp

public Service getServiceData(){
    return (Service)ServiceDaoImpl.getSession().get(Service.class, new Integer(1));
}

get方法正在获得一个。 我想获取jsp页面的所有数据。

6 个答案:

答案 0 :(得分:5)

尝试以下代码并将Entity替换为您的实体类

@SuppressWarnings("unchecked")      
public List<Entity> getAlldata(){       
    try
    {
        return sessionFactory.getCurrentSession().createCriteria(Entity.class).list();
    } catch (Exception e) {
        return new ArrayList<>();
    }
}

答案 1 :(得分:4)

尝试以下方法从表

获取所有行
@SuppressWarnings("unchecked")
    public List<Service> Service getServiceData() {

         return ServiceDaoImpl.getSession().createQuery("from Service").list(); 
    }

答案 2 :(得分:3)

public List<Service> getServiceAll(){
     return ServiceDaoImpl.getSession().createCriteria(Service.class).list();
}

答案 3 :(得分:2)

在Hibernate 5中,import numpy as np from sklearn.feature_selection import mutual_info_classif X = np.array([[0, 0, 0], [1, 1, 0], [2, 0, 1], [2, 0, 1], [2, 0, 1]]) y = np.array([0, 1, 2, 2, 1]) mutual_info_classif(X, y, discrete_features=True) # result: array([ 0.67301167, 0.22314355, 0.39575279] 已被弃用,使用类似这样的方法

createCriteria

用法

private static <T> List<T> loadAllData(Class<T> type, Session session) {
    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<T> criteria = builder.createQuery(type);
    criteria.from(type);
    List<T> data = session.createQuery(criteria).getResultList();
    return data;
  }

答案 4 :(得分:1)

配置cfg = new Configuration();         cfg.configure( “hibernate.cfg.xml中”);

    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();

    /* Selecting all objects(records) start_______________________ */



    Query qry = session.createQuery("from Product p");

    List l =qry.list();
    System.out.println("Total Number Of Records : "+l.size());
    Iterator it = l.iterator();

    while(it.hasNext())
    {
        Object o = (Object)it.next();
        Product p = (Product)o;
        System.out.println("Product id : "+p.getProductId());
        System.out.println("Product Name : "+p.getProName());
        System.out.println("Product Price : "+p.getPrice());
        System.out.println("----------------------");
    }       

    session.close();
    factory.close();

答案 5 :(得分:0)

List<Entity> list = session.createCriteria(Entity.class).list();起 被描述

您可以使用CriteriaBuilder().

        try ( Session session = sessionFactory.openSession()) {
            // Create CriteriaBuilder
            CriteriaBuilder builder = session.getCriteriaBuilder();

            // Create CriteriaQuery
            CriteriaQuery<Entity> criteria = builder.createQuery(Entity.class);

            // Specify criteria root
            criteria.from(Entity.class);

            // Execute query
            List<Entity> entityList = session.createQuery(criteria).getResultList();

            for (Entity e : entityList) {
             // load the data
            }
        }

单击here作为github示例。