-->
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.  [ 8 posts ] 
Author Message
 Post subject: [HQL] Join with fetch gives NullPointerException
PostPosted: Wed Jun 18, 2008 10:31 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
Hibernate version: 3.2.6.ga (latest)

I have a HQL problem using two fetch joins with between 2 entities and 1 value-typed component. I get a nullpointerexception while simular queries (without two fetch joins) succeed.


Here is my class setup:
A Product has only one Photo, and a photo has multiple Thumbnail formats.

The corresponding classes are:
-Product (Entity)
-Photo (Entity)
-Thumb (composed class / value type / Embeddable)

Class cardinality:
Product <--1------1--> Photo <--1-----*--> Thumb

Code:
@Entity
public class Product {
    ...
    @OneToOne
    private Photo photo;
    ...
}

Code:
@Entity
public class Photo {
    ...
    @CollectionOfElements
    private Set<Thumb> thumbs = new HashSet<Thumb>();
    ...
}

Code:
@Embeddable
public class Thumb {
    // contains only value types
}



Problem:
I want to perform a single HQL/JPA query that fetches (and initializes) the photo and all corresponding all thumbs for a given product.


I tried the query:
Code:
from Product pr
   join fetch pr.photo ph
   join fetch ph.thumbs

and
Code:
from Product pr
   join fetch pr.photo ph
   join ph.thumbs


Which both fail with:
Code:
java.lang.NullPointerException
   at org.hibernate.loader.BasicLoader.isBag(BasicLoader.java:71)
   at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:53)
   at org.hibernate.loader.hql.QueryLoader.<init>(QueryLoader.java:96)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:181)
   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.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
   at org.hibernate.console.HQLQueryPage.setSession(HQLQueryPage.java:106)


The strange thing is that the following queries succeed:
Code:
from Product pr
   join pr.photo ph
   join fetch ph.thumbs

Code:
from Photo ph
   join fetch ph.thumbs

Code:
from Product pr
   join pr.photo ph
   join ph.thumbs

Code:
from Product pr
   join fetch pr.photo ph

Code:
from Product pr
   join fetch pr.photo ph




Why do the first 2 queries fail?


Last edited by TUD on Thu Jun 19, 2008 6:11 am, edited 6 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 1:52 pm 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
Using Criteria instead of Query Language works without problems:
Code:
session.createCriteria(Product.class)
.setFetchMode("photo", FetchMode.JOIN)
.setFetchMode("thumbs", FetchMode.JOIN);


but a simular HQL query fails:
Code:
from Product pr
join fetch pr.photo ph
join fetch ph.thumbs


Anybody knows why HQL fails?


EDIT:
The Criteria doesn't seem to perform the join for thumbs when looking at the resulting SQL statement. Don't know why....


Top
 Profile  
 
 Post subject: Eclipse example to try
PostPosted: Wed Jun 18, 2008 6:16 pm 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
For those who want to the full code to see the problem, have a look at:
http://www.jtict.com/temp/HBFetchTest.zip
(Do a build before running.)

(It contains a full Eclipse project, incl. all libs and an embedded H2 DB. No setup necessary. Run TestFetch.java.)


Last edited by TUD on Thu Jun 19, 2008 6:13 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Nobody?
PostPosted: Thu Jun 19, 2008 6:02 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
So nobody who can tell why

this fails:
Code:
from Product pr
join fetch pr.photo ph
join fetch ph.thumbs

and this succeeds:
Code:
from Product pr
join pr.photo ph
join fetch ph.thumbs

???

So it must be a bug then?


Top
 Profile  
 
 Post subject: Did you found a Solution?
PostPosted: Tue Aug 12, 2008 8:31 am 
Newbie

Joined: Tue Aug 12, 2008 7:53 am
Posts: 2
Hi,

I've the same problem. Did you found a solution?

Best Regards Sebastian


Top
 Profile  
 
 Post subject: Same problem too...
PostPosted: Fri Aug 22, 2008 5:28 am 
Newbie

Joined: Fri Aug 22, 2008 5:20 am
Posts: 1
Location: Paris
Hi,
I've the same exception, with this query on hibernate 3.2.6 :

Code:
select actu
from Actualite actu
join fetch actu.rubrique rub
join rub.authorizedRoles role
where role in ( :roles )


When I remove the fetch keyword, all is ok... Looks like a bug.

A solution is more than welcome..


Top
 Profile  
 
 Post subject: I think I got it
PostPosted: Fri Aug 22, 2008 7:17 am 
Newbie

Joined: Tue Aug 12, 2008 7:53 am
Posts: 2
Hi,

I think I got it. It's because I and also TUD try do join on an embedded class. If you declare in the mapping a class as embedded, you cant join it because its no "Association" or "Reference in the hibernate termonology.
If you doo the same query with criteria instead HQL you get an correct exception. In the example above of TUD, he forgot to create an Alias for the second Fetch.

Best Regards

Sebastian


Top
 Profile  
 
 Post subject: Re: [HQL] Join with fetch gives NullPointerException
PostPosted: Sun Aug 21, 2011 7:38 pm 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
Hi i had the same problem(with Hibernate 3.6.6.Final) and i solved it like that, here the example with your query

from Product pr
join fetch pr.photo ph
join fetch pr.photo.thumbs

It seems like Hibernate does not let you use the alias when joining subelements of a one-to-one or many-to-one association. I will report a bug, because this can be a real limitation and getting behind this little secret cost me a lot of time. Hope this will help someone!

EDIT:
https://hibernate.onjira.com/browse/HHH-6593


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