Hi!
Thank you for your reply!
I'm not trying to actually get an Role object partially constructed, I'm just trying to get the permissions attribute (a set of Permission objects) for the role with id=2.
The criteria query that generated this error was built this way:
Code:
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);
Path<?> attributePath = from.get("permissions");
Predicate predicate = criteriaBuilder.equal(attributePath, new Long(2));
criteriaQuery.where(predicate);
// attempting to get just the role's permissions
CriteriaQuery<Object> select = criteriaQuery.select(attributePath);
TypedQuery<Object> typedQuery = entityManager.createQuery(select);
return typedQuery.getResultList();
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.
The Role and Permission classes have been mapped with JPA and some Hibernate annotations like this:
Code:
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'm new to criteria queries and I'm having a hard time finding what's wrong with it.
Thank you!!
Eduardo