-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Can anyone tell why is this query not valid?
PostPosted: Tue Oct 26, 2010 7:25 pm 
Newbie

Joined: Fri Oct 22, 2010 11:06 pm
Posts: 11
Hello!
I have criteria query that, when executed, results the following exception thrown:

Code:
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)


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?

It seems correct to me...

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.

Thanks!!
Eduardo Born


Top
 Profile  
 
 Post subject: Re: Can anyone tell why is this query not valid?
PostPosted: Wed Oct 27, 2010 3:24 am 
Newbie

Joined: Wed Oct 27, 2010 2:59 am
Posts: 1
can you try like " select role from temp.package.commons.user.Role as role "

you can not use few of the properties from any entity to construct any object.
just try one thing if it works like create constructor with required properties.


Top
 Profile  
 
 Post subject: Re: Can anyone tell why is this query not valid?
PostPosted: Wed Oct 27, 2010 12:10 pm 
Newbie

Joined: Fri Oct 22, 2010 11:06 pm
Posts: 11
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


Top
 Profile  
 
 Post subject: Re: Can anyone tell why is this query not valid?
PostPosted: Wed Oct 27, 2010 12:11 pm 
Newbie

Joined: Fri Oct 22, 2010 11:06 pm
Posts: 11
vivek.barsagadey, just to answer your question, "select role from temp.package.commons.user.Role as role" works fine and returns a list of Role objects as expected.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.