Retrieving all rows of a table without HQL?

前端 未结 3 1739
栀梦
栀梦 2021-01-31 13:51

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 hardco

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 14:20

    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.

提交回复
热议问题