休眠条件查询以获取特定列 [英] Hibernate Criteria Query to get specific columns

查看:28
本文介绍了休眠条件查询以获取特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用了 Criteria Query.它总是触发 select * from ...

I am using Criteria Query in my code. It always fires select * from ...

相反,我想忽略查询中的一列(字段),因为该字段以字节为单位存储了大量数据.这会导致性能问题.

Instead I want to neglect one column(field) from my query as that field have large number of data stored in bytes. And that causing performance issue.

任何人都可以给出一个想法吗?

Can any one give an idea for that?

一些更新

我在查询中添加了一个投影,它创建了一个类似于...的查询

I added a projection in my query and it created a query like...

select
    this_.TEMPLATE_ID as y0_,
    this_.TEMPLATE_NAME as y1_,
    this_.CREATE_DATE as y2_,
    this_.UPDATE_DATE as y3_,
    this_.STATUS_CODE as y4_,
    this_.USER_ID as y5_,
    this_.UPDATED_BY as y6_,
    this_.CATEGORY_ID as y7_,
    this_.PRACTICE_ID as y8_ 
from
    templates this_ 
inner join
    user user1_ 
        on this_.USER_ID=user1_.USER_ID 
inner join
    template_categories category2_ 
        on this_.CATEGORY_ID=category2_.CATEGORY_ID 
where
    y4_=? 
    and y8_=? 
    and y5_ in (
        ?, ?
    ) 
order by
    y1_ asc limit ?

现在问题就像.. 'where 子句'中的未知列'y4_'y8_ 和 y5_ 的错误相同,y5_ 意味着所有关闭的地方都会出错.

And now issue is like.. Unknown column 'y4_' in 'where clause' and same error for y8_ , y5_ means for all where close it gave an error.

我将其修改为 Query like ...

I modified it to Query like ...

select
    this_.TEMPLATE_ID as y0_,
    this_.TEMPLATE_NAME as y1_,
    this_.CREATE_DATE as y2_,
    this_.UPDATE_DATE as y3_,
    this_.STATUS_CODE as y4_,
    this_.USER_ID as y5_,
    this_.UPDATED_BY as y6_,
    this_.CATEGORY_ID as y7_,
    this_.PRACTICE_ID as y8_ 
from
    templates this_ 
inner join
    user user1_ 
        on this_.USER_ID=user1_.USER_ID 
inner join
    template_categories category2_ 
        on this_.CATEGORY_ID=category2_.CATEGORY_ID 
where
    this_.STATUS_CODE=1
    and this_.PRACTICE_ID=1 
    and this_.USER_ID in (
        1, 2
    ) 
order by
    y1_ asc limit ?

它奏效了.但是不知道怎么在HQL中修改?

and it worked. But I don't know how to modify it in HQL?

推荐答案

使用 Projections 指定您要返回的列.

Use Projections to specify which columns you would like to return.

示例

SQL 查询

SELECT user.id, user.name FROM user;

休眠替代

Criteria cr = session.createCriteria(User.class)
    .setProjection(Projections.projectionList()
      .add(Projections.property("id"), "id")
      .add(Projections.property("Name"), "Name"))
    .setResultTransformer(Transformers.aliasToBean(User.class));

  List<User> list = cr.list();

这篇关于休眠条件查询以获取特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆