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.
You can use
session.createCriteria(MyEntity.class).list();
for example.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With