-->
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.  [ 7 posts ] 
Author Message
 Post subject: Entity manager throws NullPointer with LEFT JOIN FETCH
PostPosted: Sat Aug 02, 2008 3:53 pm 
Newbie

Joined: Wed Aug 29, 2007 3:45 pm
Posts: 4
I'm having a problem with entitymanager and LEFT JOIN FETCH Queries.

Hibernate throws a NullPointerException when calling Persistence.createEntityManagerFactory("fooPU"); (i use a named query, so exception occours when Hibernate is parsing this query).

Have anyone meet with such an error.

Hibernate version:

entitymanager 3.2.2.ga with dependencies fetched by maven

Mapping documents:

Code:
@NamedQueries({
   @NamedQuery(name = "foo",
         query= "SELECT f FROM FooEntity f LEFT JOIN FETCH f.baz WHERE f.id = :id")
})
@Entity
public class FooEntity {
   @Id
   Long id;

   @Basic(fetch = FetchType.LAZY)
   String baz;

   
   public Long getId() {
      return id;
   }


   public void setId(Long id) {
      this.id = id;
   }


   public String getBaz() {
      return baz;
   }


   public void setBaz(String baz) {
      this.baz = baz;
   }
   
}


If I change foo query to:

Code:
SELECT f FROM FooEntity f FETCH ALL PROPERTIES WHERE f.id = :id


everything works OK

Full stack trace of any exception that occurs:

Code:
Exception in thread "main" java.lang.NullPointerException
   at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:310)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3275)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3067)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
   at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:402)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:352)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
   at snippet.test.main(test.java:18)


Debug level Hibernate log excerpt:

I think thats the applicable part

DEBUG - Checking 1 named HQL queries
DEBUG - Checking named query: foo
DEBUG - unable to locate HQL query plan in cache; generating (SELECT f FROM FooEntity f LEFT JOIN FETCH f.baz WHERE f.id = :id)
DEBUG - parse() - HQL: SELECT f FROM snippet.FooEntity f LEFT JOIN FETCH f.baz WHERE f.id = :id
DEBUG - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'FROM'
| | +-[RANGE] 'RANGE'
| | | +-[DOT] '.'
| | | | +-[IDENT] 'snippet'
| | | | \-[IDENT] 'FooEntity'
| | | \-[ALIAS] 'f'
| | \-[JOIN] 'JOIN'
| | +-[LEFT] 'LEFT'
| | +-[FETCH] 'FETCH'
| | \-[DOT] '.'
| | +-[IDENT] 'f'
| | \-[IDENT] 'baz'
| \-[SELECT] 'SELECT'
| \-[IDENT] 'f'
\-[WHERE] 'WHERE'
\-[EQ] '='
+-[DOT] '.'
| +-[IDENT] 'f'
| \-[IDENT] 'id'
\-[COLON] ':'
\-[IDENT] 'id'

DEBUG - throwQueryException() : no errors
DEBUG - select << begin [level=1, statement=select]
DEBUG - FromClause{level=1} : snippet.FooEntity (f) -> fooentity0_
DEBUG - Resolved : f -> fooentity0_.id
DEBUG - handling property dereference [snippet.FooEntity (f) -> baz (class)]
DEBUG - getDataType() : baz -> org.hibernate.type.StringType@e5355f
DEBUG - Resolved : f.baz -> fooentity0_.baz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 9:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

in your example baz is a simple String property. LEFT JOIN FETCH really makes only sense when trying to retrieve associated entities. So in your case FETCH ALL PROPERTIES seems to be the right query.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 10:05 am 
Newbie

Joined: Wed Aug 29, 2007 3:45 pm
Posts: 4
But what should I do to have baz fetched, withotut fetching everything else (lets suppose there are other fields that are expensive to fetch).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 11:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

normally loading the basic properties of a class all at once is no problem. Are you trying to solve a particular problem? If you are only after a particular value of your entity projections might be what you are looking for. Of course you won't get entity objects back, but Object arrays.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 11:44 am 
Newbie

Joined: Wed Aug 29, 2007 3:45 pm
Posts: 4
I have a class that has two big (@Lob) fields, and couple of others. Lobs are fetched lazily, and the others arn't. As far as I know fazy fetching of basic properties is legal. And I would like to have two queries: one gets all small properies and first Lob, second gets the second Lob, and rest of mandatory contents.

I use this in java SE enviorment and prefer to have my entity managers closed very fast, I could do:
Code:
try{
   FooEntity foo = entityManager.find();
   foo.getBaz(); //for side effect - prefetches contents of baz
}finally{
   entityManager.close();
}


But that would result in two queries sent to database. And I think there must be a way to prefetch basic property (just one; fetch all properties gets the whole lot) using EJB QL.

Well maybe there is some bad design in my database schema.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 12:26 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

lazy loading of properties is supported in Hibernate3, but requires bytecode instrumentation - http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-lazyproperties.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 12:45 pm 
Newbie

Joined: Wed Aug 29, 2007 3:45 pm
Posts: 4
Thanks!

I'll look into it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.