Hibernate多表关联查询类:
1. sql查询两个或两个以上的字段,默认情况下,list中封装的是Object[],长度与所查询的字段数一致。这种方式获取的数据只能通过index下标获取。
super.getSession().createNativeQuery(dataSql).getResultList();
2. hql 查询,在hql中使用select new 包名.类名(属性1,属性2……)将结果放入到非实体的DTO中,这种有点固化。
select new com.***.***.**DTO(属性1,属性2,属性3) from *****
3. sql查询,使用 setResultTransformer,将结果进行转化,转化成Map或者Bean对象。但5.2版本被 @Deprecated了,6版本可能会加入labada表达式。
setResultTransformer(Transformers.aliasToBean(UserDTO.class)).getResultList()
官方回复是:
For the moment, there is no replacement. In 6.0, the ResultTransformer will be replaced by a @FunctionalInterface, but the underlying mechanism of transforming the Object[] property values will probably be the same. Therefore, you can use it as-is and, only if you upgrade to 6.0, you will have to think about replacing it with the new variant.
4. hql查询或sql查询,如果返回结果为实体类(@Entity修饰与表有关联),则使用
createNativeQuery(dataSql, USERDO.class)
或者
createNativeQuery(dataSql).addEntity(USERDO.class)