说明:createQuery用的hql语句进行查询,createSQLQuery用sql语句查询;

前者以hibernate生成的Bean为对象装入list返回;
后者则是以对象数组进行存储;

一、通过createSQLQuery()查询获得,代码如下:

 String sql = "select g.*, b.name as bigTypeName, s.name as smallTypeName from t_goods g, t_bigtype b, t_smalltype s " +
"where s.id = g.smallTypeId and b.id = g.bigTypeId and g.id = :id";
Query query = this.getCurrentSession().createSQLQuery(sql);
query.setResultTransformer(Transformers.aliasToBean(GoodsBeanVo.class));
GoodsBeanVo goodsBeanVo = (GoodsBeanVo)query.uniqueResult();

注意这里要用sql语句查询,主要是

 query.setResultTransformer(Transformers.aliasToBean(GoodsBeanVo.class));

这一句代码,通过ResultTransformer这个类 AliasToBean,通过sql的查询,会返回数组,然后 hibernate根据数据表的映射,自动帮我们来set对应的字段属性,查询的值与设置的vo对应;

setResultTransformer(ResultTransformer transformer)

setResultTransformer的执行者,转换查询结果到实际应用的结果列表

ResultTransformer是个接口。

对应的GoodsBeanVo,这里的第二个构造函数可以不要。

 package com.qc.mall.vo;

 import com.qc.mall.entity.GoodsBean;

 import java.math.BigDecimal;

 /**
* @Author: sijizhen
* @Date: 2018/11/30 0030 下午 2:24
*/
public class GoodsBeanVo extends GoodsBean { private String bigTypeName;
private String smallTypeName; public GoodsBeanVo() {
} /*public GoodsBeanVo(Integer id, String name, BigDecimal price, String proPic, String brand,
Integer sales, Integer views, Integer stock, String contents, Integer bigTypeId,
Integer smallTypeId, Integer state, Object createtime, Object updatetime,
BigDecimal marketReferencePrice, String spare, String remark, String remarkFirst,
String remarkSecond, String bigTypeName, String smallTypeName) {
super(id, name, price, proPic, brand, sales, views, stock, contents, bigTypeId,
smallTypeId, state, createtime, updatetime, marketReferencePrice,
spare, remark, remarkFirst, remarkSecond);
this.bigTypeName = bigTypeName;
this.smallTypeName = smallTypeName;
}*/ public String getBigTypeName() {
return bigTypeName;
} public void setBigTypeName(String bigTypeName) {
this.bigTypeName = bigTypeName;
} public String getSmallTypeName() {
return smallTypeName;
} public void setSmallTypeName(String smallTypeName) {
this.smallTypeName = smallTypeName;
}
}

二、通过createQuery()查询获得,代码如下:

 String hql = "select new com.qc.mall.vo.GoodsBeanVo(g.id, g.name, g.price, g.proPic, g.brand, g.sales, g.views, g.stock, g.contents, g.bigTypeId," +
"g.smallTypeId, g.state, g.createtime, g.updatetime, g.marketReferencePrice," +
"g.spare, g.remark, g.remarkFirst, g.remarkSecond, b.name, s.name) from GoodsBean g, BigTypeBean b, SmallTypeBean s " +
"where s.id = g.smallTypeId and b.id = g.bigTypeId and g.id = :id";
Query query = this.getCurrentSession().createQuery(hql);
query.setParameter("id", id);
GoodsBeanVo goodsBeanVo = (GoodsBeanVo)query.uniqueResult();

用createQuery()查询,需要vo有对应的构造函数,且hql语句中参数的顺序以及参数的类型都要与构造函数中的一致,如果参数类型中有时间类型,需要做相应的转换,否则会报错:

Unable to locate appropriate constructor on class

报此类错误时具体可以参考:http://blog.sina.com.cn/s/blog_4ad7c2540102uzkc.html

对应的GoodsBeanVo代码如下:

 package com.qc.mall.vo;

 import com.qc.mall.entity.GoodsBean;

 import java.math.BigDecimal;

 /**
* @Author: sijizhen
* @Date: 2018/11/30 0030 下午 2:24
*/
public class GoodsBeanVo extends GoodsBean { private String bigTypeName;
private String smallTypeName; public GoodsBeanVo() {
} public GoodsBeanVo(Integer id, String name, BigDecimal price, String proPic, String brand,
Integer sales, Integer views, Integer stock, String contents, Integer bigTypeId,
Integer smallTypeId, Integer state, Object createtime, Object updatetime,
BigDecimal marketReferencePrice, String spare, String remark, String remarkFirst,
String remarkSecond, String bigTypeName, String smallTypeName) {
super(id, name, price, proPic, brand, sales, views, stock, contents, bigTypeId,
smallTypeId, state, createtime, updatetime, marketReferencePrice,
spare, remark, remarkFirst, remarkSecond);
this.bigTypeName = bigTypeName;
this.smallTypeName = smallTypeName;
} public String getBigTypeName() {
return bigTypeName;
} public void setBigTypeName(String bigTypeName) {
this.bigTypeName = bigTypeName;
} public String getSmallTypeName() {
return smallTypeName;
} public void setSmallTypeName(String smallTypeName) {
this.smallTypeName = smallTypeName;
}
}

如有纰漏,还望指正。

参考:https://blog.csdn.net/qq_38286331/article/details/81391948

参考:https://blog.csdn.net/richerg85/article/details/41745819

Hibernate查询返回自定义VO的两种方式的更多相关文章

  1. EntityFramework Core 2.0自定义标量函数两种方式

    前言 上一节我们讲完原始查询如何防止SQL注入问题同时并提供了几种方式.本节我们继续来讲讲EF Core 2.0中的新特性自定义标量函数. 自定义标量函数两种方式 在EF Core 2.0中我们可以将 ...

  2. iOS 自定义layer的两种方式

    在iOS中,你能看得见摸得着的东西基本都是UIView,比如一个按钮,一个标签,一个文本输入框,这些都是UIView: 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层 在创建UIVi ...

  3. Asp.net Web API 返回Json对象的两种方式

    这两种方式都是以HttpResponseMessage的形式返回, 方式一:以字符串的形式 var content = new StringContent("{\"FileName ...

  4. Android Activity返回键控制的两种方式

    Android Activity返回键监听的两种方式 1.覆写Activity的OnBackPressed方法 官方解释: Called when the activity has detected ...

  5. 自定义UITabBar的两种方式

    开发中,经常会遇到各种各样的奇葩设计要求,因为apple提供的UITabBar样式单一,只是简单的"图片+文字"样式,高度49又不可以改变.自定义UITabBar成为了唯一的出路. ...

  6. SpringBoot自定义过滤器的两种方式及过滤器执行顺序

    第一种 @WebFilter + @ServletComponentScan 注解 1.首先自定义过滤器 如下自定义过滤器 ReqResFilter 必须实现  javax.servlet.Filte ...

  7. JavaWeb 返回json数据的两种方式

    1.说明 由于一般情况下,由浏览器(前端)发送请求,服务器(后台)响应json数据,所以这里结合js进行说明: A服务器发送请求至B服务器,并接收其返回的json数据,见文末推荐,这里不再赘述! 2. ...

  8. AntDesign VUE:上传组件自定义限制的两种方式(Boolean、Promise)

    AntD上传组件 AntDesign VUE文档 第一种方式 beforeUpload(file) { let isLt = true if (filesSize) { isLt = file.siz ...

  9. 关于Spring Data JPA 多表查询 返回自定义Vo的问题记录

    这两天开了一个新项目,使用SpringBoot+SpringData,  刚做了一个小功能,都是一张表的操作没什么问题,今天设计到了两张表联查,两张表各取了几个字段,组合成了一个vo, 当我用原生sq ...

随机推荐

  1. PS教您与粗壮的胳膊拜拜

    Step 01在Photoshop 中打开素材图片,图中圈出的地方是需要调整的. Step 02用[套索工具]圈出胳膊及周围的环境. Step 03单击右键,选择[羽化],设置[羽化半径]为20 像素 ...

  2. 第二部分之AOF持久化(第十一章)

    AOF持久化是通过保存Redis服务器所执行的写命令来记录数据库状态的.被写入AOF文件的所有命令都是以Redis的命令请求协议格式(纯文本)保存的. 一,AOF持久化的实现 1.命令追加 当AOF持 ...

  3. HDU 3518 Boring counting

    题目:Boring counting 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 题意:给一个字符串,问有多少子串出现过两次以上,重叠不能算两次 ...

  4. jsp中【<%=request.getContextPath()%>】项目路径

    1 2 "request.getContextPath()的值是        "<%=request.getContextPath()%><br/> &q ...

  5. redis一主二从加哨兵

    redis版本:redis-3.0.6.tar.gz master:192.168.3.180 slave:192.168.3.184 (机器原因,两从都在这上面) 一.redis安装 cd /roo ...

  6. idea下创建maven聚合(子父级)项目,多模块项目

    IDEA下Maven多模块项目介绍和搭建 idea 创建maven聚合项目简洁教程(手把手入门,通俗易懂) 本人使用的是: intelj idea 创建聚合项目(典型web项目,包括子项目util.d ...

  7. linux中去掉^M的方法

    转:https://blog.csdn.net/sty945/article/details/80347901 (1)是用VI的命令: 在命令模式下运行命令 :%s/^M//g 回车 注意:手动输入该 ...

  8. OOM实例

    1. 使用Executors.newFixedThreadPool()方法,当不断创建新任务,而任务执行速度比创建速度慢时,任务对象就会在任务队列里面排队,堆内存得不到释放,导致OOM: 2. 使用P ...

  9. cas单点登录-https的配置(一)

     前言   由于个人的兴趣和为了加薪,在这里研究下cas单点登录,同时也记录下自己探索的过程,希望也能帮到有同样兴趣的小伙伴 环境 CAS-5.1.3 tomcat8.5 jdk8 centos6.5 ...

  10. Android 入门(2)修改EditText下划线颜色 / 隐藏标题栏

    1 添加颜色 colors.xml中增加 <color name="colorRed">#FF3300</color> 2 添加style styles.x ...