You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Tomasz Mazan <wi...@wp.pl> on 2007/11/26 17:04:19 UTC

Force to load subobjects in OpenJPA

Hi

I got one class Product which contains a few ArayLists of different
subobjects. Default fetch policy is set to LAZY, but sometimes I have to 
use Product with its all subobjects. I'm accessing remotely to my DAO layer
(using @Remote interfaces) so findBy.... returns "detached" object by
default. 

How can I force DAO layer (that uses EntityManager) to initialize found
object with all its subobjects ?

thanks in advance
Beniamin
-- 
View this message in context: http://www.nabble.com/Force-to-load-subobjects-in-OpenJPA-tf4876000s134.html#a13952442
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.


Re: Force to load subobjects in OpenJPA

Posted by Tomasz Mazan <wi...@wp.pl>.
Oh, You've just sent solution :) I should had refresh nabble page before i've
post my message :)

Beniamin


Jacek Laskowski wrote:
> 
> On Nov 27, 2007 3:45 PM, Tomasz Mazan <wi...@wp.pl> wrote:
> 
>> 1) if I use query like
>>
>> @NamedQueries({
>>         @NamedQuery(name = "ProductWithDeps",
>>                     query = "SELECT p FROM Product p"
>>                                 + " WHERE p.number = :number")
>> })
>> product is found and returned, but if I use query like:
>>
>> 2)
>> @NamedQueries({
>>         @NamedQuery(name = "ProductWithDeps",
>>                     query = "SELECT p FROM Product p"
>>                             + " join fetch p.blocks"
>>                                 + " WHERE p.number= :number")
>> })
>> I get null as a finder result. What's wrong ?
> 
> Most likely the entity doesn't have any blocks and thus the query
> returns null (p.blocks evaluates to null, and the query returns only
> entities that have at least one block in them - it's how INNER join
> works). Add LEFT before JOIN FETCH and it should work as expected.
> 
> @NamedQueries({
>        @NamedQuery(name = "ProductWithDeps",
>                    query = "SELECT p FROM Product p"
>                            + " LEFT JOIN FETCH p.blocks"
>                                + " WHERE p.number= :number")
> })
> 
> Jacek
> 
> -- 
> Jacek Laskowski
> http://www.JacekLaskowski.pl
> 
> 

-- 
View this message in context: http://www.nabble.com/Force-to-load-subobjects-in-OpenJPA-tf4876000s134.html#a13972749
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.


Re: Force to load subobjects in OpenJPA

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On Nov 27, 2007 3:45 PM, Tomasz Mazan <wi...@wp.pl> wrote:

> 1) if I use query like
>
> @NamedQueries({
>         @NamedQuery(name = "ProductWithDeps",
>                     query = "SELECT p FROM Product p"
>                                 + " WHERE p.number = :number")
> })
> product is found and returned, but if I use query like:
>
> 2)
> @NamedQueries({
>         @NamedQuery(name = "ProductWithDeps",
>                     query = "SELECT p FROM Product p"
>                             + " join fetch p.blocks"
>                                 + " WHERE p.number= :number")
> })
> I get null as a finder result. What's wrong ?

Most likely the entity doesn't have any blocks and thus the query
returns null (p.blocks evaluates to null, and the query returns only
entities that have at least one block in them - it's how INNER join
works). Add LEFT before JOIN FETCH and it should work as expected.

@NamedQueries({
       @NamedQuery(name = "ProductWithDeps",
                   query = "SELECT p FROM Product p"
                           + " LEFT JOIN FETCH p.blocks"
                               + " WHERE p.number= :number")
})

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl

Re: Force to load subobjects in OpenJPA

Posted by Tomasz Mazan <wi...@wp.pl>.
OK, working fine :)
I should use left join fetch cause of empty sets of dependent objects.
Thanks for your help

Beniamin


Tomasz Mazan wrote:
> 
> Jacek
> Thanks for your advice. I use named queries and...
> 
> 1) if I use query like
> 
> @NamedQueries({
> 	@NamedQuery(name = "ProductWithDeps", 
> 	            query = "SELECT p FROM Product p" 
> 	            		+ " WHERE p.number = :number")	            		
> })
> product is found and returned, but if I use query like:
> 
> 2)
> @NamedQueries({
> 	@NamedQuery(name = "ProductWithDeps",
> 	            query = "SELECT p FROM Product p" 
> 	            	    + " join fetch p.blocks"
> 	            		+ " WHERE p.number= :number")	            		
> })
> I get null as a finder result. What's wrong ?
> 
> Beniamin
> 
> 
> Jacek Laskowski wrote:
>> 
>> On Nov 26, 2007 5:04 PM, Tomasz Mazan <wi...@wp.pl> wrote:
>> 
>>> How can I force DAO layer (that uses EntityManager) to initialize found
>>> object with all its subobjects ?
>> 
>> Use named queries with FETCH JOINs?
>> 
>> Jacek
>> 
>> -- 
>> Jacek Laskowski
>> http://www.JacekLaskowski.pl
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Force-to-load-subobjects-in-OpenJPA-tf4876000s134.html#a13972650
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.


Re: Force to load subobjects in OpenJPA

Posted by Tomasz Mazan <wi...@wp.pl>.
Jacek
Thanks for your advice. I use named queries and...

1) if I use query like

@NamedQueries({
	@NamedQuery(name = "ProductWithDeps", 
	            query = "SELECT p FROM Product p" 
	            		+ " WHERE p.number = :number")	            		
})
product is found and returned, but if I use query like:

2)
@NamedQueries({
	@NamedQuery(name = "ProductWithDeps",
	            query = "SELECT p FROM Product p" 
	            	    + " join fetch p.blocks"
	            		+ " WHERE p.number= :number")	            		
})
I get null as a finder result. What's wrong ?

Beniamin


Jacek Laskowski wrote:
> 
> On Nov 26, 2007 5:04 PM, Tomasz Mazan <wi...@wp.pl> wrote:
> 
>> How can I force DAO layer (that uses EntityManager) to initialize found
>> object with all its subobjects ?
> 
> Use named queries with FETCH JOINs?
> 
> Jacek
> 
> -- 
> Jacek Laskowski
> http://www.JacekLaskowski.pl
> 
> 

-- 
View this message in context: http://www.nabble.com/Force-to-load-subobjects-in-OpenJPA-tf4876000s134.html#a13972140
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.


Re: Force to load subobjects in OpenJPA

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On Nov 26, 2007 5:04 PM, Tomasz Mazan <wi...@wp.pl> wrote:

> How can I force DAO layer (that uses EntityManager) to initialize found
> object with all its subobjects ?

Use named queries with FETCH JOINs?

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl