-->
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: elements() in simple HQL requires object alias?
PostPosted: Thu Apr 12, 2007 7:16 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm using a simple named query to search for all objects that have a given value-element in a set. It works if I alias the object, but not if I don't. In the code below, query OrganisationsByType1 produces the given exception, OrganisationsByType2 works perfectly.

I've done a quick search through this forum and through JIRA, but I couldn't find anything like this. Does anyone know if this has been fixed, or even if there's a JIRA issue about it that I missed? I am using a fairly old version of hibernate, and I don't expect any fix to be applied to 3.0.5 since there's such an easy workaround, but it should be fixed in future versions.

Hibernate version: 3.0.5

Mapping documents:
Code:
  <class name="OrganisationImpl" table="Organisation">
  ...
    <set name="Types" table="OrganisationTypeLink" lazy="true" cascade="all">
      <key column="OrganisationID"/>
      <element column="OrganisationTypeID" not-null="true"
       type="OrganisationType"/>
    </set>
  </class>

  <query name="OrganisationsByType1">
    from OrganisationImpl
    where :Type in elements(Types)
  </query>

  <query name="OrganisationsByType2">
    from OrganisationImpl o
    where :Type in elements(o.Types)
  </query>


Full stack trace of any exception that occurs:
Code:
java.lang.NullPointerException
   at org.hibernate.hql.ast.MethodNode.handleElements(MethodNode.java:154)
   at org.hibernate.hql.ast.MethodNode.resolveCollectionProperty(MethodNode.java:105)
   at org.hibernate.hql.ast.CollectionFunction.resolve(CollectionFunction.java:22)
   at org.hibernate.hql.ast.HqlSqlWalker.processFunction(HqlSqlWalker.java:538)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.collectionFunction(HqlSqlBaseWalker.java:2273)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.collectionFunctionOrSubselect(HqlSqlBaseWalker.java:3937)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.inRhs(HqlSqlBaseWalker.java:3854)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3517)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1405)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1333)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:599)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:404)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
   at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
   at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:388)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)
        ...

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 8:55 pm 
Beginner
Beginner

Joined: Thu Apr 12, 2007 10:43 am
Posts: 21
Location: Brazil
I don't really think that's a bug - isn't it mandatory to assign aliases whenever you want to refer to the properties of an instance in more than one place of your query?

http://www.hibernate.org/hib_docs/v3/re ... ryhql.html

I understand that this kind of implicit reference is OK in SQL, but IMHO the alias requirement leads to easier to read, less error-prone code. Cheers,
Roger


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 9:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It is a bug: you can omit the alias, I do it all the time. There's even a few examples in the link you posted, including
Code:
from Cat where name='Fritz'
.

It's not easier to read with the alias, unless the alias is the same as the mapping name (like "from Cat cat where cat.name = 'Fritz'", or whatever). How does "from Cat sdlkjfsdbfh where sdlkjfsdbfh.name" look? Not so great, imo.

This is definitely a bug, and should be resovled. Though it's a very minor bug.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 7:30 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 10:43 am
Posts: 21
Location: Brazil
Hmm, you're correct. I read the third example in section 14.2 and thought there were no exceptions. Most definitely a bug, indeed - fortunately there's an easy workaround. I understand your point about aliases, but you gotta admit that using "sdlkjfsdbfh" for an alias only makes your life harder ;) Cheers,
Roger


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.