views:

71

answers:

2

Hi!

I'm having a hard time constructing a criteria query to get the "permissions" attribute from the Role entity with id = 2. The permission attribute is of Set type, so I'm creating a join and selecting from it, but the query fails with invalid grammar exception reporting "Unknown column '2L' in 'where clause'"

The criteria query that generates the error was built this way:

EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();

Class<?> queryScopeClass = temp.pack.commons.user.Role.class;

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

Path<?> idAttrPath = from.get("id");
// also tried criteriaBuilder.equal(attributePath, new Long(2))
Predicate predicate = criteriaBuilder.equal(attributePath, criteriaBuilder.literal(new Long(2)))
criteriaQuery.where(predicate);


Path<?> attributePath = from.get("permissions");
PluralAttributePath<?> pluralAttrPath = (PluralAttributePath<?>)attributePath;
PluralAttribute<?, ?, ?> pluralAttr = pluralAttrPath.getAttribute();


Join<?, ?> join = from.join((SetAttribute<Object,?>)pluralAttr);

TypedQuery<Object> typedQuery = entityManager.createQuery(criteriaQuery.select(join));
return (List<P>)typedQuery.getResultList();

When I execute the line to actually return the results, the following exception is thrown:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:250)
    at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getResultList(CriteriaQueryCompiler.java:260)
    at temp.pack.dao.impl.DefaultDAOService.getProperties(DefaultDAOService.java:628)
...
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2452)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
    at org.hibernate.loader.Loader.list(Loader.java:2187)
    at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:936)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
    ... 20 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '2L' in 'where clause'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2264)
    at com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:179)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1869)
    at org.hibernate.loader.Loader.doQuery(Loader.java:718)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    ... 27 more

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;

...

}

What is wrong with this criteria query?

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 (and initialize) 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.

Follow up: Here is the query that was Hibernate is trying to execute:

select permission1_.PERMISSION_ID as PERMISSION1_12_,
    permission1_.IS_REQUIRED as IS2_12_,
    permission1_.SOURCE_ROLE_ID as SOURCE3_12_,
    permission1_.TARGET_ROLE_ID as TARGET4_12_
from (
        select ROLE_ID,
        NAME,
        DESCRIPTION,
        IS_ACTION,
        LABEL,
        null as FIRST_NAME,
        null as LAST_NAME,
        null as PASSWORD_HASH,
        1 as clazz_ from GROUPS
    union
        select ROLE_ID,
            NAME,
            null as DESCRIPTION,
            null as IS_ACTION,
            null as LABEL,
            FIRST_NAME,
            LAST_NAME,
            PASSWORD_HASH,
            2 as clazz_ from USERS
    )
role0_ inner join PERMISSIONS permission1_ on role0_.ROLE_ID=permission1_.SOURCE_ROLE_ID
    where (role0_.ROLE_ID=2L )

Clearly the problem is with the 2L. The 2L is really a string or a column from MySQL's perspective since it has that L on it and is not quoted.

Is this a bug in Hibernate? It looks like one to me. That L should not be in the generated SQL Query...

Anyone has any ideas of what that is happening? It seems to me that this would be a major bug that couldn't pass unnoticed by everyone else, so I'm assuming there is a good change I'm doing something wrong.

Thank you!!

Eduardo

A: 

hi, you can try using alias for the two tables that you have used to create the join in criteria. This can also be the problem as hibernate only creates alias for HQL,s and entity loads but for criteria is rely on user to create the same.

Mrityunjay
This cannot be it, Hibernate creates aliases for criteria queries. I posted the query, now the real question is whether I did something wrong or this is a bug in Hibernate.
Eduardo Born