我是靠谱客的博主 虚拟西装,这篇文章主要介绍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

复制代码
1
2
Query query = session.createQuery("from Cat cat inner join Owner owner where owner.Name ='Duke'");

No defined association in hbm.xml file

复制代码
1
2
3
4
5
6
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

复制代码
1
2
3
4
5
6
7
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

复制代码
1
2
3
4
5
6
7
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

复制代码
1
2
3
4
5
6
7
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)]
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这是我报的错误,报错的原因属于语法格式的错误,整个HQL语句也在这里, 结合网上查找的解决办法,总结如下: 1、多余的空格; 2、字符串引号应该为单引号; 3、=:应该是在一起的,中间没有空格,‘like :’中介需要有空格, 其实这里没这么严谨,只是以防万一; 4、括号,我出的问题就是左括号比右括号多了一个; 5、一条SQL或者HQL语句中只能有一个order by(在没有子查询的前提下) ,所以如果有多个需要参照的排序条件, 那么就在order by后加上,每个条件之间用逗号隔开,比如: from Topic t order by t.postTime desc,t.lastUpdatedTime asc

最后

以上就是虚拟西装最近收集整理的关于QuerySyntaxException:unexpected token: ON [null] near line 1, column 135的全部内容,更多相关QuerySyntaxException:unexpected内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部