Can anyone tell why this Hibernate query is invalid?
I have criteria query that, when executed, results the following exception thrown:
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)
As can be seen in the code box, the query that raises the error is:
select generatedAlias0.permissions from temp.package.commons.user.Role as generatedAlias0 where generatedAlias0.id=2L
The id attribute is the entity's attribute which was annotated with @Id @GeneratedValue. Basically I'm trying to load the attribute "permissions", which is a collection uninitialized at the time, for the role object whose id is 2. The id attribute is of long type.
Any ideas on why this query would fail?
I can post the code that creates the criteria query here, but it is fairly complicated (as it generates based on a LDAP filter, etc), so I'm hoping the error will be visible from the generated query string and the exception. Please let me know if that's not the case.
Follow-up: The criteria query that generated this error was built this way:
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();
The Role and Permission classes have been mapped with JPA and some Hibernate annotations like this:
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;
...
}
I was expecting the call to typedQuery.getResultList() to return a list of collections with just one element: the collection of permission objects for the role with id = 2. This is an attempt to select just the "permissions" collection from the object role with id = 2.
I'm new to criteria queries and I'm having a hard time finding what's wrong with it.
Without more information, it sounds like you are trying to do something like:
select rp
from Role r
inner join r.permissions rp
where r.id = 2
Can you post your annotated entities and a sample of your Criteria?
精彩评论