Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving all rows of a table without HQL?

Tags:

hibernate

I am using Hibernate 4 and would like to simply list all rows of a table. All solutions I found suggest using something like "from tablename", but I would like to avoid hardcoding tablenames in strings.

like image 698
Marcus Riemer Avatar asked Jan 31 '12 17:01

Marcus Riemer


2 Answers

You can use

session.createCriteria(MyEntity.class).list();

for example.

like image 115
mrab Avatar answered Sep 20 '22 23:09

mrab


HQL doesn't use table names. It uses entity names. And entity names are (by default) class names. So you can use

String hql = "select a from " + TheEntity.class.getSimpleName() + " a";

But I would favor readability over type safety here, and use

String hql = "select a from TheEntity a";

You should have automated tests for your queries anyway.

like image 38
JB Nizet Avatar answered Sep 22 '22 23:09

JB Nizet