1 2:select(table.*)/(全部列)OK
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”工作正常,并将结果映射到Student.class.
喜欢:Student [id =?name =?sex =?(其他字段为空/默认)]
我不知道这个错误,请帮助我!
解决方法
你可以进一步添加
.setResultTransformer(Transformers.aliasToBean(YOUR_DTO.class));
并自动将其映射到您的自定义dto对象,另见 Returning non-managed entities.
.setResultTransformer(Transformers.aliasToBean(YOUR_DTO.class));
并自动将其映射到您的自定义dto对象,另见 Returning non-managed entities.
例如:
public List<MessageExtDto> getMessagesForProfile2(Long userProfileId) { Query query = getSession().createsqlQuery(" " + " select a.*,b.* " + " from messageVO AS a " + " INNER JOIN ( SELECT max(id) AS id,count(*) AS count FROM messageVO GROUP BY messageConversation_id) as b ON a.id = b.id " + " where a.id > 0 " + " ") .addScalar("id",new LongType()) .addScalar("message",new StringType()) ......... your mappings .setResultTransformer(Transformers.aliasToBean(MessageExtDto.class)); List<MessageExtDto> list = query.list(); return list; }