关于HQL的一个报错

zxy16305 2017-09-08 09:55:41
在bean中,包含了一个集合属性,然后查询出错了。


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这是hql查询语句

List list = session.createQuery(//
"SELECT new Post(e.id,e.title,e.commentsList) FROM Post e WHERE e.id < 5")//
.list();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这是bean

public class Post {
private Integer id;
private String title;
private String message;
private Set<Comments> commentsList = new TreeSet<Comments>();
//省略一大堆setter和getter
public Post(){ }
public Post(Integer id, String title, Set<Comments> commentsList) {
this.id = id;
this.title = title;
this.commentsList = commentsList;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
配置文件中两个类的映射关系

<set name="commentsList" table="Comments">
<key column="postID"> </key>
<one-to-many class="top.kiswich.class2table.Comments"/>
</set>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<many-to-one name="post" class="top.kiswich.class2table.Post" column="postID"> </many-to-one>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
以及报错信息
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as col_2_0_ from post post0_ inner join Comments commentsli1_ on post0_.id=comme' at line 1

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as col_2_0_ from post post0_ inner join Comments commentsli1_ on post0_.id=comme' at line 1
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
生成的sql语句

Hibernate:
select
post0_.id as col_0_0_,
post0_.title as col_1_0_,
. as col_2_0_
from
post post0_
inner join
Comments commentsli1_
on post0_.id=commentsli1_.postID
where
post0_.id<5

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
大佬们起床了吗
...全文
207 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxy16305 2017-09-10
  • 打赏
  • 举报
回复
嗯.用对象数组或者直接from都行,但就是有点强迫症,这个new贼好用
lifewell1 2017-09-08
  • 打赏
  • 举报
回复
commentsList这个是有问题的 new () 这个方式 hql是不认识下级对象的 本质上是sql转对象 你这样肯定不可以 你还不如直接from就好 不用专门去转
zxy16305 2017-09-08
  • 打赏
  • 举报
回复
您好 这样是可以了。。不过用new对象的方式怎么做呢
青蛙Keroro 2017-09-08
  • 打赏
  • 举报
回复
SELECT new Post(e.id,e.title,e.commentsList) FROM Post e WHERE e.id < 5 SELECT id,title,commentsList FROM Post WHERE id < 5

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧