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 ...
随机推荐
- h5-音视频标签
###1. <video>:Html5提供的播放视频的标签 src:资源地址 controls:该属性定义是显示还是隐藏用户控制界面 调用默认控件 ...
- 【经典数据结构】B树与B+树
本文转载自:http://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.html 维基百科对B树的定义为“在计算机科学中,B树 ...
- ADT SDK Manager启动时一闪而过
原因为使用了Android Studio的绿色JRE,必须要安装安装版JDK或者JRE,绿色版JRE放在ADT目录虽然能启动ADT但是不能启动SDK Manager
- git 回退各种场景操作
在git的一般使用中,如果发现错误的将不想提交的文件add进入index之后,想回退取消,则可以使用命令:git reset HEAD <file>...,同时git add完毕之后,gi ...
- [WC2018]通道——边分治+虚树+树形DP
题目链接: [WC2018]通道 题目大意:给出三棵n个节点结构不同的树,边有边权,要求找出一个点对(a,b)使三棵树上这两点的路径权值和最大,一条路径权值为路径上所有边的边权和. 我们按照部分分逐个 ...
- MT【322】绝对值不等式
已知 $a,b,c\in\mathbb R$,求证:$|a|+|b|+|c|+|a+b+c|\geqslant |a+b|+|b+c|+|c+a|$ 分析:不妨设$c=\max\{a,b,c\},\d ...
- Python--logging模块不同级别写入到不同文件
将不同级别的logging 日志信息写入到不同文件 # -*- coding: utf-8 -*- import os import time import logging import inspec ...
- jmeter笔记(1)--原理,下载与安装
Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文 ...
- shell 基础(一)
废话少说 往下看 1. 查看 Shell Shell 是一个程序,一般都是放在/bin或者/user/bin目录下,当前 Linux 系统可用的 Shell 都记录在/etc/shells文件中./e ...
- ElasticSearch6.5.0 【Java客户端之REST Client】
说明 High Level Client 是基于 Low Level Client 的.官方文档如下: * https://www.elastic.co/guide/en/elasticsearch/ ...