2010-10-27 129 views
0

我的條件查詢,在執行時,會導致以下異常拋出:任何人都可以告訴爲什麼這個Hibernate查詢無效嗎?

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的角色對象加載當時未初始化的屬性「權限」.ID屬性是長類型的。

關於爲什麼這個查詢會失敗的任何想法?

我可以在這裏發佈創建條件查詢的代碼,但它相當複雜(因爲它基於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的角色的權限對象集合。這是一個嘗試只選擇「perm issions「collection from the object role with id = 2.

我是新來的標準查詢,我很難找到它有什麼問題。

+0

什麼數據類型是權限? – 2010-10-27 01:58:53

回答

2

沒有更多的信息,這聽起來像你正在嘗試做的是這樣的:

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

您可以發佈您註明實體和你的標準的樣本?

相關問題