Hibernate Criteria Query来获取特定的列 [英] Hibernate Criteria Query to get specific columns

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

问题描述

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



相反,我想忽略查询中的一列(字段)因为该字段具有以字节存储的大量数据。










$ b

某些更新



我在查询中添加了投影,并创建了一个查询,如...

 选择
this_.TEMPLATE_ID如y0_,
this_.TEMPLATE_NAME如y1_,
this_.CREATE_DATE作为y2_ ,
this_.UPDATE_DATE为y3_,
this_.STATUS_CODE为y4_,
this_.USER_ID为y5_,
this_.UPDATED_BY为y6_,
this_.CATEGORY_ID为y7_ ,
this_.PRACTICE_ID as y8_
from
模板this_
内部连接
用户user1_
on this_.USER_ID = user1_.USER_ID
内部加入
template_categories category2_
on this_.CATEGORY_ID = category2_.CATEGORY_ID
where
y4_ =?
和y8_ =?
和y5_ in(
?,?

order by
y1_ asc limit?

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



我将它修改为Query ... ...

  select 
this_.TEMPLATE_ID as y0_,
this_.TEMPLATE_NAME as y1_,
this_.CREATE_DATE为y2_,
this_.UPDATE_DATE为y3_,
this_.STATUS_CODE为y4_,
this_.USER_ID为y5_,
this_.UPDATED_BY为y6_,
this_.CATEGORY_ID如y7_,
this_.PRACTICE_ID作为y8_
。从
模板THIS_
内加入
用户user1_
上this_.USER_ID = user1_.USER_ID
内连接上this_.CATEGORY_ID = category2_.CATEGORY_ID

template_categories category2_
,其中
this_.STATUS_CODE = 1个
和this_.PRACTICE_ID = 1
和this_.USER_ID(
1,2

订单
y1_ asc limit ?

并且工作正常。但我不知道如何在HQL中修改它?

使用

解决方案

jboss.org/hibernate/core/3.6/javadocs/org/hibernate/criterion/Projections.htmlrel =noreferrer> Projections 来指定您想要返回的列。



示例

SQL查询

  SELECT user.id,user.name FROM user; 

Hibernate Alternative

  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 = cr.list();


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?


Some Update

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 ?

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.

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 ?

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

解决方案

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

Example

SQL Query

SELECT user.id, user.name FROM user;

Hibernate Alternative

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();

这篇关于Hibernate Criteria Query来获取特定的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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