在列上使用别名查询会导致错误 [英] Query using alias on column give an error

查看:236
本文介绍了在列上使用别名查询会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为列使用别名时,出现错误。没有别名everytinig工程很好。那有什么问题?这是一个简单的例子,但是需要在实际项目中使用更多的别名来包装某些非实体类的结果,但不能因为这个错误。如何解决这个问题?


$ b 不工作(在id列上有别名):

  public List< Long> findAll(Long ownerId){
String sql =从其中ownerId =+ ownerId;
SQLQuery query = getSession()。createSQLQuery(sql);
返回query.list();

错误:


WARN [JDBCExceptionReporter:77]:SQL错误:0,SQLState:S0022错误
[JDBCExceptionReporter:78]:找不到列'id'。

WORKING (不含别名):

 公开列表< Long> findAll(Long ownerId){
String sql =从ownerId =+ ownerId;
SQLQuery query = getSession()。createSQLQuery(sql);
返回query.list();


解决方案

映射,休眠可能不知道myId,因此无法选择它。
您可以尝试如下所示:

  getSession()。createSQLQuery(sql).addScalar(myId,Hibernate .LONG)


When i use alias for column i get error. Without alias everytinig works good. What is the problem with that ? This is simple example, but need to use more aliases in real project to wrap results in some not-entity class, but can't because of this error. How to solve this ?

NOT WORKING (with alias on id column):

public List<Long> findAll(Long ownerId) {
    String sql = "select id as myId  from products where ownerId = "+ownerId;
    SQLQuery query = getSession().createSQLQuery(sql);
    return query.list();
}

Error:

WARN [JDBCExceptionReporter:77] : SQL Error: 0, SQLState: S0022 ERROR [JDBCExceptionReporter:78] : Column 'id' not found.

WORKING (without alias):

public List<Long> findAll(Long ownerId) {
    String sql = "select id from products where ownerId = "+ownerId;
    SQLQuery query = getSession().createSQLQuery(sql);
    return query.list();
}

解决方案

If your "product" is mapped, hibernate probably don't know about "myId" and therefore can't select it. You can try something like:

getSession().createSQLQuery(sql).addScalar("myId", Hibernate.LONG)

这篇关于在列上使用别名查询会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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