Hibernate查询返回自定义VO的两种方式

本文详细比较了在Hibernate框架中使用HQL和SQL进行查询的区别。通过具体的代码示例,阐述了createQuery与createSQLQuery两种方法的应用场景,以及如何处理查询结果。特别关注了查询结果转换为业务对象的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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

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

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

setResultTransformer(ResultTransformer transformer) 

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

ResultTransformer是个接口。

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

 1 package com.qc.mall.vo;
 2 
 3 import com.qc.mall.entity.GoodsBean;
 4 
 5 import java.math.BigDecimal;
 6 
 7 /**
 8  * @Author: sijizhen
 9  * @Date: 2018/11/30 0030 下午 2:24
10  */
11 public class GoodsBeanVo extends GoodsBean {
12 
13     private String bigTypeName;
14     private String smallTypeName;
15 
16     public GoodsBeanVo() {
17     }
18 
19     /*public GoodsBeanVo(Integer id, String name, BigDecimal price, String proPic, String brand,
20                        Integer sales, Integer views, Integer stock, String contents, Integer bigTypeId,
21                        Integer smallTypeId, Integer state, Object createtime, Object updatetime,
22                        BigDecimal marketReferencePrice, String spare, String remark, String remarkFirst,
23                        String remarkSecond, String bigTypeName, String smallTypeName) {
24         super(id, name, price, proPic, brand, sales, views, stock, contents, bigTypeId,
25                 smallTypeId, state, createtime, updatetime,  marketReferencePrice,
26                 spare, remark, remarkFirst, remarkSecond);
27         this.bigTypeName = bigTypeName;
28         this.smallTypeName = smallTypeName;
29     }*/
30 
31     public String getBigTypeName() {
32         return bigTypeName;
33     }
34 
35     public void setBigTypeName(String bigTypeName) {
36         this.bigTypeName = bigTypeName;
37     }
38 
39     public String getSmallTypeName() {
40         return smallTypeName;
41     }
42 
43     public void setSmallTypeName(String smallTypeName) {
44         this.smallTypeName = smallTypeName;
45     }
46 }

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

1 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," +
2               "g.smallTypeId, g.state, g.createtime, g.updatetime,  g.marketReferencePrice," +
3               "g.spare, g.remark, g.remarkFirst, g.remarkSecond, b.name, s.name) from GoodsBean g, BigTypeBean b, SmallTypeBean s " +
4               "where s.id = g.smallTypeId and b.id = g.bigTypeId and g.id = :id";
5 Query query = this.getCurrentSession().createQuery(hql);
6 query.setParameter("id", id);
7 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代码如下:

 1 package com.qc.mall.vo;
 2 
 3 import com.qc.mall.entity.GoodsBean;
 4 
 5 import java.math.BigDecimal;
 6 
 7 /**
 8  * @Author: sijizhen
 9  * @Date: 2018/11/30 0030 下午 2:24
10  */
11 public class GoodsBeanVo extends GoodsBean {
12 
13     private String bigTypeName;
14     private String smallTypeName;
15 
16     public GoodsBeanVo() {
17     }
18 
19     public GoodsBeanVo(Integer id, String name, BigDecimal price, String proPic, String brand,
20                        Integer sales, Integer views, Integer stock, String contents, Integer bigTypeId,
21                        Integer smallTypeId, Integer state, Object createtime, Object updatetime,
22                        BigDecimal marketReferencePrice, String spare, String remark, String remarkFirst,
23                        String remarkSecond, String bigTypeName, String smallTypeName) {
24         super(id, name, price, proPic, brand, sales, views, stock, contents, bigTypeId,
25                 smallTypeId, state, createtime, updatetime,  marketReferencePrice,
26                 spare, remark, remarkFirst, remarkSecond);
27         this.bigTypeName = bigTypeName;
28         this.smallTypeName = smallTypeName;
29     }
30 
31     public String getBigTypeName() {
32         return bigTypeName;
33     }
34 
35     public void setBigTypeName(String bigTypeName) {
36         this.bigTypeName = bigTypeName;
37     }
38 
39     public String getSmallTypeName() {
40         return smallTypeName;
41     }
42 
43     public void setSmallTypeName(String smallTypeName) {
44         this.smallTypeName = smallTypeName;
45     }
46 }

如有纰漏,还望指正。

参考:https://blog.youkuaiyun.com/qq_38286331/article/details/81391948

参考:https://blog.youkuaiyun.com/richerg85/article/details/41745819

转载于:https://www.cnblogs.com/sijizhen/p/10057294.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值