错误的hql查询

时间:2014-07-21 13:12:16

标签: java mysql sql hibernate hql

我有以下实体:

@Entity
public class Item {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

@ManyToOne
private User ownerUser;

@ManyToMany
private Set<User> grantedUsers;

当我运行以下查询时:

select Item from Item d, d.grantedUsers u where ownerUser.id= :id or u.id =:id

我看到错误消息:

[2014-07-21 15:07:31] d.grantedUsers is not mapped [select Item from com.test.Item d, d.grantedUsers u where ownerUser.id= :id or u.id =:id]

实体映射在hibernate / spring *.xml文件中。

有什么问题?

4 个答案:

答案 0 :(得分:1)

select Item from Item d, d.grantedUsers u where ownerUser.id= :id or u.id =:id

这整个查询对我来说很奇怪。我敢打赌,Hibernate试图在d.grantedUsers子句中威胁from作为实体类(未映射到whitch)。试试这个:

select d from Item d where ownerUser.id= :id or d.grantedUsers.id =:id

答案 1 :(得分:0)

指定反向关系并使用mappedBy

答案 2 :(得分:0)

class User{
 ....

 @ManyToOne
 @JoinColumn(name="Item ")
 private Item item;
 ...

}

FROM User where id= :grantedUsersIds or id = :ownerUserids

你的User类的User类中应该有很多关系,从上面的查询中你可以得到你所有的项目

答案 3 :(得分:0)

来自http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

的hi​​bernate文档
select d from Item d join d.grantedUsers u 
    where d.ownerUser.id = :id or u.id = :id