Java - Hibernate criteria.setResultTransformer() initializes model fields with default values

北城余情 提交于 2019-11-28 23:30:46

You probably simply forgot to assign aliases to your projections:

Projections.projectionList()
           .add(Projections.property("id"), "id")
           .add(Projections.property("name"), "name")

In addition and to respond to @Ram comment :

Projections.projectionList()
       .add(Projections.property("id"), "id")
       .add(Projections.property("name"), "name")

"id" and "name" are Java field name in your Student class, but in your database they are named "StudentId" and "Name", moreover hibernate generate SQL query with random alias to avoid name conflict, so in result set there are not "id" and "name" column. The second parameter in the above example force the alias name.

Hibernate generate SQL like that :

SELECT students0_.StudientId as StudientId12, students0_.Name as Name34, students0_.Grade as Grade11 FROM Students students0_

You can tell hibernate to display generated SQL query in console/log by setting hibernate.show_sql to true in Hibernate config file hibernate.cfg.xml.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!