当前位置:编程学习 > JAVA >>

hibernate 查指定字段报错

查询是这样写的:
		StringBuffer hql = new StringBuffer("select new Archive (a.id, "
+ "a.releaseDate, " + "a.user, " + "a.attribute, "
+ "a.title, " + "a.keywords, " + "a.description, "
+ "a.updated, " + "a.click, " + "a.picname, " + "a.deleted, "
+ "a.isStatic, "
+ "a.fileName, a.category, a.comment) from Archive a");

文章 Archive Bean 构造器是这样的:
public Archive(Integer id, Date releaseDate, User user, String attribute,
String title, String keywords, String description, Date updated,
Integer click, String picname, Integer deleted, char isStatic,
String fileName, Category category, Set<Comment> comment) {
this.id = id;
this.releaseDate = releaseDate;
this.user = user;
this.attribute = attribute;
this.title = title;
this.keywords = keywords;
this.description = description;
this.updated = updated;
this.click = click;
this.picname = picname;
this.deleted = deleted;
this.isStatic = isStatic;
this.fileName = fileName;
this.category = category;
this.comment = comment;
}

其中Set<Comment>是评论,与Comment是双向一对多的关系,我想在查询时将它也查询出来,但总说我的hql有问题,如果去掉Comment类似的关联就对了
我该如何解决这个问题? --------------------编程问答-------------------- 顶一顶,精神抖擞 --------------------编程问答-------------------- 两个表连起来,把你的关系表也放在from里面 --------------------编程问答--------------------
引用 2 楼 fujia305 的回复:
两个表连起来,把你的关系表也放在from里面

兄弟,生动点哇。 --------------------编程问答-------------------- 报的原始错误是什么,你贴上来。 --------------------编程问答-------------------- 好。错误信息来也:
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_14_0_ from caobug_archives archive0_ inner join caobug_users user1_ on ar' at line 1 --------------------编程问答-------------------- 学习大虾精神,貌似还未解决 --------------------编程问答-------------------- 这个应该是它生成的SQL语句
Hibernate: select archive0_.id as col_0_0_, archive0_.releaseDate as col_1_0_, archive0_.uid as col_2_0_, archive0_.attribute as col_3_0_, archive0_.title as col_4_0_, archive0_.keywords as col_5_0_, archive0_.description as col_6_0_, archive0_.updated as col_7_0_, archive0_.click as col_8_0_, archive0_.picname as col_9_0_, archive0_.deleted as col_10_0_, archive0_.is_static as col_11_0_, archive0_.file_name as col_12_0_, archive0_.tid as col_13_0_, . as col_14_0_ from caobug_archives archive0_ inner join caobug_users user1_ on archive0_.uid=user1_.id inner join caobug_category category2_ on archive0_.tid=category2_.id inner join caobug_comment comment3_ on archive0_.id=comment3_.id order by archive0_.releaseDate desc limit ? --------------------编程问答--------------------
引用 5 楼 lingduit 的回复:
好。错误信息来也:
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_14_0_ from caobug_archives archive0_ inner join caobug_users user1_ on ar' at line 1


sql语句出现问题 把sql语句copy出来丢到数据库里运行检查错误 --------------------编程问答-------------------- Hibernate: select archive0_.id as col_0_0_, archive0_.releaseDate as col_1_0_, archive0_.uid as col_2_0_, archive0_.attribute as col_3_0_, archive0_.title as col_4_0_, archive0_.keywords as col_5_0_, archive0_.description as col_6_0_, archive0_.updated as col_7_0_, archive0_.click as col_8_0_, archive0_.picname as col_9_0_, archive0_.deleted as col_10_0_, archive0_.is_static as col_11_0_, archive0_.file_name as col_12_0_, archive0_.tid as col_13_0_, . as col_14_0_ from caobug_archives archive0_ inner join caobug_users user1_ on archive0_.uid=user1_.id inner join caobug_category category2_ on archive0_.tid=category2_.id inner join caobug_comment comment3_ on archive0_.id=comment3_.id order by archive0_.releaseDate desc limit ? 
这里就出现问题了,但HQL生成的语句这是这样呢?? --------------------编程问答-------------------- 你的那个关系型comment不要用,如果你要引用的话也得去关系表里,它本身就是关系,你用hibernate做映射的时候你配置的它的属性应该是对应的关系表,你不能这样单独的引用它,所以你的hql里面不能直接有它,有它也是从关系表引出的
from Archive   a inner join comment c on a.comment = c.comment  
select里面最好是c.comment
你的java类也需要改 --------------------编程问答-------------------- 你还没了解hibernate的原理啊
既然你配置了关系
那么你查archive出来
然后archive对象里面就已经能直接拿到Comment的信息了
比如说
我查出来的是archive
我直接就能archive.comment
而且这个根据你用get跟load会有延迟加载的区别
根本不用在hql语句里面写上comment
而且你这个hql写的这样我看起来有点蛋疼啊
你唯一定位一条数据
需要这么多字段呢? --------------------编程问答-------------------- hibernate是一个o/r mapping 框架
他做的就是把表转换成对象了
你直接把查数据当做操作对象就好
你查出来a
a里面不管有B,C,D多少个关联对象
你都能直接点出来
不要再写什么条件去查
不然要你配置关联做什么
hibernate的优势不就在这吗 --------------------编程问答--------------------
引用 12 楼 djy18178 的回复:
hibernate是一个o/r mapping 框架
他做的就是把表转换成对象了
你直接把查数据当做操作对象就好
你查出来a
a里面不管有B,C,D多少个关联对象
你都能直接点出来
不要再写什么条件去查
不然要你配置关联做什么
hibernate的优势不就在这吗

谢谢兄弟指教,我现在的需求是不查询某些字段,比如网站首页查询文章是不需要展示查询正文的~ --------------------编程问答--------------------
引用 13 楼 lingduit 的回复:
Quote: 引用 12 楼 djy18178 的回复:

hibernate是一个o/r mapping 框架
他做的就是把表转换成对象了
你直接把查数据当做操作对象就好
你查出来a
a里面不管有B,C,D多少个关联对象
你都能直接点出来
不要再写什么条件去查
不然要你配置关联做什么
hibernate的优势不就在这吗

谢谢兄弟指教,我现在的需求是不查询某些字段,比如网站首页查询文章是不需要展示查询正文的~

这样啊 那你根据特定字段查回来的就是数组哦 只写查询那个对象的条件就行了 关联对象不用写条件了 hibernate帮你找的 --------------------编程问答-------------------- Hibernate里可以用SQL语句,不用HQL语句。建议用SQL语句试试
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,