Java hibernate selects multiple rows from the table and assigns the results to the list
I want to select multiple rows / records from the table and put the results into variables Then I want to print each line
If I use hibernate in Java, what should I do?
For example, I have a student table. I want to get all the rows of the table I know that each row is an instance of a table / entity class / bean (suppose I have a physical class – students)
This table is like (suppose I have a data table in the database):
Table name: sutdent
There are four pillars
id,lname,fname,sex
I remember I could do this:
List<student> stlist=HibernateUtil.getSession().createQuery("select*from student").list(); //here I want to print the lname of the first row/object System.out.println(stlist.get(0).getLname(););
Why do I receive the error message "token not specified *"?
I can't use *? What should I do if I can't use *, if I want to get all the contents of the record (all properties / columns)?
Or are there other mistakes? What should I do? thank you!!
Solution
In HQL, this is how you write a query to get a list of all objects: –
List<student> stlist = HibernateUtil.getSession().createQuery("select s from student s").list();
If you want to get all the columns, you are even allowed to do so
List<student> stlist = HibernateUtil.getSession().createQuery("from student s").list();