在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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
大佬们起床了吗