任何人都可以告诉为什么这个Hibernate查询无效?

时间:2010-10-27 01:44:15

标签: java hibernate syntax criteria

我有条件查询,在执行时会导致抛出以下异常:

java.lang.IllegalArgumentException: org.hibernate.QueryException: illegal syntax near collection: id [select generatedAlias0.permissions from temp.package.commons.user.Role as generatedAlias0 where generatedAlias0.id=2L]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1222)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:320)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:227)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:437)
at temp.package.dao.impl.DefaultDAOService.getProperties(DefaultDAOService.java:585)

从代码框中可以看出,引发错误的查询是:

select generatedAlias0.permissions from temp.package.commons.user.Role as generatedAlias0 where generatedAlias0.id=2L

id属性是实体的属性,用@Id @GeneratedValue注释。基本上我正在尝试为id为2的角色对象加载属性“permissions”,这是一个未初始化的集合.id属性是long类型。

有关此查询失败原因的任何想法?

我可以在这里发布创建条件查询的代码,但它相当复杂(因为它基于LDAP过滤器等生成),所以我希望从生成的查询字符串中可以看到错误例外。如果情况不是这样,请告诉我。

跟进:生成此错误的条件查询是以这种方式构建的:

EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();

// queryScopeClass is assigned to type temp.pack.commons.user.Role
Class<? extends T> queryScopeClass = role.getClass();

Root<? extends T> from = criteriaQuery.from(queryScopeClass);

Predicate predicate = criteriaBuilder.equal(from.get("id"), new Long(2));
criteriaQuery.where(predicate);

// attempting to get just the role's permissions
CriteriaQuery<Object> select = criteriaQuery.select(from.get("permissions"));
TypedQuery<Object> typedQuery = entityManager.createQuery(select);

return typedQuery.getResultList();

角色和权限类已使用JPA和一些Hibernate注释进行映射,如下所示:

public abstract class Role implements Serializable {

 /**
  * The id of this role. Internal use only.
  * 
  * @since 1.0
  */
 @Id @GeneratedValue
 protected long id;

 /**
  * Set of permissions granted to this role.
  * 
  * @since 1.0
  */
 @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy="sourceRole")
 protected Set<Permission> permissions = new HashSet<Permission>();

...

}

public class Permission implements Serializable {
 private static final long serialVersionUID = 1L;

 /**
  * The id of this permission. Used internally for persistence.
  * 
  * @since 1.0
  */
 @Id @GeneratedValue
 @Column(name = "PERMISSION_ID")
 protected long id;

 /**
  * The group to which the owner of this permission is being granted permission to.
  * 
  * @since 1.0
  */
 @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
 @JoinColumn(name = "TARGET_ROLE_ID")
 @ForeignKey(name = "FK_TARGET_GROUP_PERMISSION_ID",
   inverseName = "FK_PERMISSION_ID_TARGET_GROUP")
 protected Group targetGroup;

 /**
  * The role that has been granted this permission.
  * 
  * @since 1.0
  */
 @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
 @JoinColumn(name = "SOURCE_ROLE_ID")
 @ForeignKey(name = "FK_SOURCE_GROUP", inverseName = "FK_GROUP_PERMISSIONS")
 private Role sourceRole;

...

}

我希望调用typedQuery.getResultList()来返回只有一个元素的集合列表:id = 2的角色的权限对象集合。这是尝试仅选择“权限”集合来自id为2的对象角色。

我是标准查询的新手,我很难找到它的错误。

1 个答案:

答案 0 :(得分:2)

如果没有更多信息,您可能会尝试执行以下操作:

select rp
from Role r
inner join r.permissions rp
where r.id = 2

您可以发布带注释的实体和标准样本吗?