休眠 SQL 查询结果映射/转换为对象/类/Bean
2022-09-02 12:33:56
1 2:选择(表.*)/(所有列)是否正常
String sql = "select t_student.* from t_student";
//String sql = "select t_student.id,t_student.name,... from t_student"; //select all column
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Student.class);//or query.addEntity("alias", Student.class);
//query.list();[Student@..., Student@..., Student@...]
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); //or other transformer
query.list(); //[{Student(or alias)=Student@...},{Student=Student@...}]
3:选择一些列(不是全部),是错误
String sql = "select t_student.id,t_student.name.t_student.sex from t_student";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Student.class);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
query.list(); //Exception:invalid column/no column
我希望“3”工作正常,并让结果可以映射到学生.class。
比如:Student[id=?, name=?,=?, (其他字段为空/默认值)]
我不知道这个错误,请帮帮我!