本文介绍了在列上使用别名查询会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我为列使用别名时,出现错误。没有别名everytinig工程很好。那有什么问题?这是一个简单的例子,但是需要在实际项目中使用更多的别名来包装某些非实体类的结果,但不能因为这个错误。如何解决这个问题?
$ b 不工作(在id列上有别名):
public List< Long> findAll(Long ownerId){
String sql =从其中ownerId =+ ownerId;
SQLQuery query = getSession()。createSQLQuery(sql);
返回query.list();
错误:
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:
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)
这篇关于在列上使用别名查询会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!