QuerySyntaxException:unexpected token: ON [null] near line 1, column 135

Getting error that unexpected token: ON near line 1, column 135

解决方式:
No defined association in hbm.xml file

Hibernate HQL Inner Join

Query query = session.createQuery("from Cat cat inner join Owner 
owner where owner.Name ='Duke'");

No defined association in hbm.xml file

Query query = session.createQuery("from Cat cat inner join Owner
 owner on cat.OwnerId = owner.Id where owner.Name='Duke'");
But the query will fail with the following error message:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: 
unexpected token: on near line 1, column xx

Native SQL is the solution

import org.hibernate.SQLQuery;
...
SQLQuery query = session.createSQLQuery("SELECT cat.* from cat inner join owner
 on cat.owner_id = owner.id where owner.name=:username");
query.addEntity(Cat.class);
query.setInteger("username",'Duke');
List =query.list();

hibernate框架在映射数据库表时,是将表转换为实体类,用操作数据对象代替操作表,这跟java语言面向对面象的操作原来一样,hql提供更加丰富灵活、更为强大的查询能力,hql语句对应实体类和实体类的属性进行了封装,查询语句from后面接的不是表名称,而是javabean实体对象名,where条件后面的不是表字段,而是javabean实体属性名。

session.createQuery(sql);
session.createSQLQuerysql);


QuerySyntaxException: unexpected token: user_id near line 1, column 29

QuerySyntaxException: unexpected token: user_id near line 1, column 29
 [from model.Demand d where d user_id=? order by d.id desc]

查询语法错

user_id在Demand的User中 
 正确的写法from Demand d where d.user.id=? order by d.id desc

unexpected token: null near line 1, column 290

org.hibernate.hql.internal.ast.QuerySyntaxException: 
unexpected token: null near line 1, column 290 
[select count(*) from cn.com.taiji.sample.entity.User t 
where 1=1 and (t.name like :userName or t.namePy like :userName or
t.loginName like :userName and t.status =:status 
and not exists(select b.user from cn.com.sample.entity.UserRole b 
where b.role.id =:roleId and b.user.id = t.id)]
这是我报的错误,报错的原因属于语法格式的错误,整个HQL语句也在这里,
结合网上查找的解决办法,总结如下:

1、多余的空格;

2、字符串引号应该为单引号;

3、=:应该是在一起的,中间没有空格,‘like :’中介需要有空格,
其实这里没这么严谨,只是以防万一;

4、括号,我出的问题就是左括号比右括号多了一个;

5、一条SQL或者HQL语句中只能有一个order by(在没有子查询的前提下)
,所以如果有多个需要参照的排序条件,
那么就在order by后加上,每个条件之间用逗号隔开,比如:
from Topic t order by t.postTime desc,t.lastUpdatedTime asc

你可能感兴趣的:(BUG收割机)