Hibernate查询返回自定义VO的两种方式
说明: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的两种方式的更多相关文章
- EntityFramework Core 2.0自定义标量函数两种方式
前言 上一节我们讲完原始查询如何防止SQL注入问题同时并提供了几种方式.本节我们继续来讲讲EF Core 2.0中的新特性自定义标量函数. 自定义标量函数两种方式 在EF Core 2.0中我们可以将 ...
- iOS 自定义layer的两种方式
在iOS中,你能看得见摸得着的东西基本都是UIView,比如一个按钮,一个标签,一个文本输入框,这些都是UIView: 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层 在创建UIVi ...
- Asp.net Web API 返回Json对象的两种方式
这两种方式都是以HttpResponseMessage的形式返回, 方式一:以字符串的形式 var content = new StringContent("{\"FileName ...
- Android Activity返回键控制的两种方式
Android Activity返回键监听的两种方式 1.覆写Activity的OnBackPressed方法 官方解释: Called when the activity has detected ...
- 自定义UITabBar的两种方式
开发中,经常会遇到各种各样的奇葩设计要求,因为apple提供的UITabBar样式单一,只是简单的"图片+文字"样式,高度49又不可以改变.自定义UITabBar成为了唯一的出路. ...
- SpringBoot自定义过滤器的两种方式及过滤器执行顺序
第一种 @WebFilter + @ServletComponentScan 注解 1.首先自定义过滤器 如下自定义过滤器 ReqResFilter 必须实现 javax.servlet.Filte ...
- JavaWeb 返回json数据的两种方式
1.说明 由于一般情况下,由浏览器(前端)发送请求,服务器(后台)响应json数据,所以这里结合js进行说明: A服务器发送请求至B服务器,并接收其返回的json数据,见文末推荐,这里不再赘述! 2. ...
- AntDesign VUE:上传组件自定义限制的两种方式(Boolean、Promise)
AntD上传组件 AntDesign VUE文档 第一种方式 beforeUpload(file) { let isLt = true if (filesSize) { isLt = file.siz ...
- 关于Spring Data JPA 多表查询 返回自定义Vo的问题记录
这两天开了一个新项目,使用SpringBoot+SpringData, 刚做了一个小功能,都是一张表的操作没什么问题,今天设计到了两张表联查,两张表各取了几个字段,组合成了一个vo, 当我用原生sq ...
随机推荐
- SpringCloud搭建Eureka集群
第一部分:搭建Eureka Server集群 Step1:新建工程,引入依赖 依赖文件pom.xml如下 <?xml version="1.0" encoding=" ...
- 本文详解5G是个什么鬼,程序员都准备好了吗?
无线移动通讯发展历史 最近5G的概念炒的如火如荼,为此,华为和高通还干了一仗.这篇文章从技术层面给大家分析,什么是5G,它和4G比,高级在哪里? 我们来看看移动互联网的技术发展: 然后我们在来看看他们 ...
- Linux配置外网访问mysql
stream{ upstream abc{ server 192.168.8.249:3306; } server{ listen 9211 ; prox ...
- 其它综合-有趣的linux命令行工具-lolcat
lolcat :一个在 Linux 终端中输出彩虹特效的命令行工具 何为Lolcat Lolcat 是一个针对 Linux,BSD 和 OSX 平台的工具,它类似于 cat,并为 cat 的输出添加彩 ...
- sql-josn
1 select fname,fdistrict ,famount from sale for json auto---最简单方式[{"name":"name1" ...
- 三、调试IIS启动域名配置
一.IIS配置启动VS以及域名 1.hosts配置 2.配置 注意: 1.Web和Api 端口在IIS都设置80即可,都可以同时运行不冲突,与vs的IIS express启动方式不同vs会指定不同的两 ...
- js 实现数据结构 -- 栈
原文: 在 Javascript 中学习数据结构与算法. 概念: 栈是一种遵从先进后出 (LIFO) 原则的有序集合:新添加的或待删除的元素都保存在栈的末尾,称作栈顶,另一端为栈底.在栈里,新元素都靠 ...
- MT【320】依次动起来
已知$ BC=6,AC=2AB, $点$ D $满足$ \overrightarrow{AD}=\dfrac{2x}{x+y}\overrightarrow{AB}+\dfrac{y}{2(x+y)} ...
- [PA2014]Druzyny
题目描述 体育课上,n个小朋友排成一行(从1到n编号),老师想把他们分成若干组,每一组都包含编号连续的一段小朋友,每个小朋友属于且仅属于一个组.第i个小朋友希望它所在的组的人数不多于d[i],不少于c ...
- Python import hook
转自http://blog.konghy.cn/2016/10/25/python-import-hook/,这里有好多好文章 import hook 通常被译为 探针.我们可以认为每当导入模块的时候 ...