You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Hibernate 5高效获取数据库表中所有行数据?

替代Hibernate 5中过时createCriteria的全表数据获取方案

Hey there, you're right that session.createCriteria(Student.class).list() is deprecated in Hibernate 5 — the team has shifted focus to standard JPA APIs and more modern query patterns. Let me share a few efficient, up-to-date solutions to fetch all rows from your student table:

方案一:JPA Criteria API(推荐,符合标准)

This is the official recommended replacement for the old Hibernate Criteria API, as it follows JPA standards and makes your code more portable across different JPA providers.

If you're using Hibernate's Session interface:

try (Session session = sessionFactory.openSession()) {
    CriteriaBuilder cb = session.getCriteriaBuilder();
    CriteriaQuery<Student> cq = cb.createQuery(Student.class);
    // 指定查询的实体类
    cq.from(Student.class);
    
    List<Student> allStudents = session.createQuery(cq).getResultList();
    // 在这里处理获取到的学生列表
}

If you're working with JPA's EntityManager (more aligned with modern practices):

try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> cq = cb.createQuery(Student.class);
    Root<Student> studentRoot = cq.from(Student.class);
    cq.select(studentRoot);
    
    TypedQuery<Student> query = entityManager.createQuery(cq);
    List<Student> allStudents = query.getResultList();
    // 处理数据逻辑
}

方案二:HQL查询(简洁直接)

For simple, static queries where you don't need dynamic filtering, HQL is the most straightforward option with minimal code:

try (Session session = sessionFactory.openSession()) {
    // 简单的HQL语句,直接查询所有Student实体
    String hql = "FROM Student";
    Query<Student> query = session.createQuery(hql, Student.class);
    List<Student> allStudents = query.getResultList();
    // 处理数据
}

Or with JPA EntityManager:

try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
    TypedQuery<Student> query = entityManager.createQuery("SELECT s FROM Student s", Student.class);
    List<Student> allStudents = query.getResultList();
    // 处理数据
}

方案三:原生SQL查询(仅适合特殊场景)

While not necessary for basic full-table fetching, you can use native SQL if you need to execute database-specific queries:

try (Session session = sessionFactory.openSession()) {
    String nativeSql = "SELECT * FROM student";
    NativeQuery<Student> query = session.createNativeQuery(nativeSql, Student.class);
    List<Student> allStudents = query.getResultList();
    // 处理数据
}

额外小贴士

  • Always use try-with-resources blocks for Session or EntityManager — this ensures resources are automatically closed, avoiding memory leaks.
  • Prioritize the JPA Criteria API for future-proof, portable code; use HQL for simple, readable queries.

内容的提问来源于stack exchange,提问作者Mani R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:25:12