日期:2014-05-17  浏览次数:20746 次

hql 多对多关系 查询表的部分字段
新闻表
     id,title,time, content
类型表
     id,name

这两个表是多对多关系,现在只想查 新闻的id,title,time,不想查 content字段,如何查询?
 如果(select new News(id,title,time) from News n inner join fetch n.newsTypes t where t.id=1)
空指针  求高手指教

------解决方案--------------------
实例类是什么样的?

多对多应该有中间表吧?
------解决方案--------------------
 new News(id,title,time) 这里需要使用别名.属性的方式:n.id,n.title........
------解决方案--------------------
没有外键关联么?
------解决方案--------------------
引用:
Quote: 引用:

 new News(id,title,time) 这里需要使用别名.属性的方式:n.id,n.title........

不行 这样就报错了 

报错了?什么错?
------解决方案--------------------
引用:
Quote: 引用:

实例类是什么样的?

多对多应该有中间表吧?

字段已经列出来了,有个中间表,


String hql = "select n.id,n.title,n.time from News n inner join n.newsTypes t where t.id = 1";
------解决方案--------------------
query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties
这个错误提示了,不是new那里的问题。
问题是:where t.id=1,n.newsTypes t,我忘了是否能这么给别名,但是t是集合,t.id=1是个什么意思?明显的语法错误
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

实例类是什么样的?

多对多应该有中间表吧?

字段已经列出来了,有个中间表,


String hql = "select n.id,n.title,n.time from News n inner join n.newsTypes t where t.id = 1";

这个方法好像不行哎
报错
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{expli。。。。

你原来的查询语句有fetch ,删除了吗?
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

实例类是什么样的?

多对多应该有中间表吧?

字段已经列出来了,有个中间表,


String hql = "select n.id,n.title,n.time from News n inner join n.newsTypes t where t.id = 1";

这个方法好像不行哎
报错
org.hibernate.QueryException: query specified join fetching, but