【问题标题】:Query scalar collections in HQL在 HQL 中查询标量集合
【发布时间】:2011-03-26 06:49:27
【问题描述】:

我有以下课程:

class User {
String username;
@CollectionOfElements
private Set<String> roles = new HashSet<String>();
[many more things here]
}

我想编写一个 HQL 查询来检索所有用户的用户名和角色。

Query query = session.createQuery("select distinct u.username, u.roles from User u");

但它不起作用,它会抛出以下异常:

org.hibernate.QueryException: not an entity [select distinct u.username,u.roles from com.eyeprevent.domain.users.User u]

抱怨u.roles 不是实体。

我怎样才能实现我想要的? (查询where u.roles='arole' 也是可能的,但同样不起作用)。

【问题讨论】:

    标签: java hibernate orm collections hql


    【解决方案1】:

    你在找这个吗:

    select u.username 
      from User u
     where :role in elements(u.roles)
    

    【讨论】:

    • 也许可以。我会在星期一试试。谢谢。如何选择用户的角色?也许select u.username, elements(u.roles) from User u
    猜你喜欢
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多