Hibernate get List from database

前端 未结 6 1169
盖世英雄少女心
盖世英雄少女心 2021-02-07 22:31

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

public List getAllProducts() throws          


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 22:52

    Your answer not only adds a cast, but switches from SQL to HQL. Since your 2nd query is in HQL, Hibernate is able to use mapping information to know what class to return. This is the preferred way to do things in Hibernate, but if you had to use SQL for some reason you could achieve the same thing with:

    (List)session.createSQLQuery("SELECT * FROM Products").addEntity(Products.class).list();
    

提交回复
热议问题