我在代码中使用了条件查询.它总是emits select * from ...

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

有人能给出一个 idea 吗?


Some Update

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

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 ?

现在的问题是..Unknown column 'y4_' in 'where clause'

我把它改成了...

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中修改它?

推荐答案

使用Projections指定要返回的列.

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

Mysql相关问答推荐

在SQL中连接两个表,从第一个表中 Select 随机行

MySQL InnoDB:可以在没有回滚损失的情况下从运行的查询中进行大型插入吗

配置MySQL服务器以管理数千个表

左联接重复问题

如何为Oracle DB查询获得所需的GROUP BY结果?

MySQL逻辑全部

插入时发生日期时间字段溢出错误

表列中的SQL SUM MENY值记录单个查询

时间戳上滚动窗口的 SQL 计数不同

按优先级 for each 具有 status_id 的员工 Select 单行

Mysql如何分行分词

如何在不使用子查询的情况下按最小日期计算新用户?

MySQL如何通过外键ID Select 多行?

从 SQL 中的左连接和内连接中减go 计数

Select 在同一天售出不同活动门票的收银员

如何使用 phpmyadmin 复制数据库?

为什么数据库行元组中的整数具有L后缀?

如何从 MySQL Workbench 中的图表生成 SQL 脚本?

在连接条件上使用 IS NULL 或 IS NOT NULL - 理论问题

Mysql中int(10)的最大大小是多少