Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Dynamic fetch plan with navigations
Dynamic fetch plan with navigations [message #379026] Thu, 07 August 2008 06:59 Go to next message
Frank Schwarz is currently offline Frank SchwarzFriend
Messages: 26
Registered: July 2009
Location: Dresden
Junior Member
Is it possible to declare a query such that fields of associated objects
are guaranteed to be loaded?

Lets say you have: Catalog <-->n Article <-->n Description
You query for Catalog and want to have certain fields from Article and
Description to be fetched as well.

I am aware of JPA fetch joins. However, due to a subtle omission in the
JPA query grammar, those fetch joins cannot be cascaded.

I also had a look at chapter "Using Queries with Fetch Groups" in the
EclipseLink documentation[1], but I cannot figure out how to specify
object-navigations here.

Kindly appreciate your help,
Frank


[1]
http://wiki.eclipse.org/Using_Advanced_Query_API_(ELUG)#How_to_Query_with_a_Dynamic_Fetch_Group
Re: Dynamic fetch plan with navigations [message #379028 is a reply to message #379026] Thu, 07 August 2008 12:52 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
You can specify nested fetch joins through the query hint,
"eclipselink.join-fetch".

i.e.
JPQL: "Select e from Employee e"
Hint: "eclipselink.join-fetch"="e.manager.phoneNumbers"

Fetch groups do not support cascading yet, although their precursor
partial attribute queries do, you could look into these as well, but they
do not allow caching or updates.

I think an option to force instantiation would be useful without requiring
joining (such as using batch reading), so feel free to add an enhancement
request. You can also add one for nested fetch groups if you wish.

-- James
Re: Dynamic fetch plan with navigations [message #379029 is a reply to message #379028] Thu, 07 August 2008 15:28 Go to previous messageGo to next message
Frank Schwarz is currently offline Frank SchwarzFriend
Messages: 26
Registered: July 2009
Location: Dresden
Junior Member
James, thank you for this hint.

> JPQL: "Select e from Employee e"
> Hint: "eclipselink.join-fetch"="e.manager.phoneNumbers"

I tried it, however it did not take effect in my case. I suppose that
cascading 1-n-relationships are not supported, i.e.
"e.subordinates.phoneNumbers".


> I think an option to force instantiation would be useful without requiring
> joining (such as using batch reading), so feel free to add an enhancement
> request. You can also add one for nested fetch groups if you wish.

It is quite hard for me to come up with a enhancement request for fetch
groups. I mean, the JDO2 expert group accepted this challenge but created
something with only academic relevance (to say the least). On the other
hand, the JPA expert group seams to be reluctant to standardize fetch
groups at all. Maybe the eagerly awaited Criteria query API will include
something of relevance.

-- Frank
Re: Dynamic fetch plan with navigations [message #379035 is a reply to message #379029] Mon, 11 August 2008 13:11 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Cascaded 1-m fetch joins are supported, but generally not a good idea
because of the extent of the data duplication. In general I would
recommend batch reading over fetch joining, there is also a hint for it.

What SQL did you get from using the hint?

-- James
Re: Dynamic fetch plan with navigations [message #379039 is a reply to message #379035] Mon, 11 August 2008 15:38 Go to previous messageGo to next message
Frank Schwarz is currently offline Frank SchwarzFriend
Messages: 26
Registered: July 2009
Location: Dresden
Junior Member
> What SQL did you get from using the hint?

Unfortunately, I cannot reproduce the observed behavior. It was like
pre-fetching the first relationship and later fetching on-demand the
nested relationship.

Now I am seeing an exception with the cascading query hint.
Object model: Person with self-reference "friends" as many-to-many
Query:
Query query = entityManager.createQuery("SELECT p FROM Person p");
query.setHint("eclipselink.join-fetch", "p.friends.friends");

resulting SQL:

SELECT t2.ID, t2.FIRSTNAME, t2.LASTNAME, t0.ID, t0.FIRSTNAME, t0.LASTNAME
FROM FRIEND t4, FRIEND t3, PERSON t2, PERSON t1, PERSON t0 WHERE
(((t3.Person_ID = t2.ID) AND (t1.ID = t3.Friend_ID)) AND ((t4.Person_ID =
t1.ID) AND (t0.ID = t4.Friend_ID)))

Exception:
Exception in thread "main" java.lang.NullPointerException
at
org.eclipse.persistence.mappings.ForeignReferenceMapping.pre pareNestedJoinQueryClone(ForeignReferenceMapping.java:1491)
at
org.eclipse.persistence.mappings.CollectionMapping.valueFrom RowInternalWithJoin(CollectionMapping.java:1560)
at
org.eclipse.persistence.mappings.ForeignReferenceMapping.val ueFromRow(ForeignReferenceMapping.java:1338)
at
org.eclipse.persistence.mappings.ForeignReferenceMapping.bui ldCloneFromRow(ForeignReferenceMapping.java:171)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildAttributesIntoWorkingCopyClone(ObjectBuilder.java:1238)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildWorkingCopyCloneFromRow(ObjectBuilder.java:1355)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObjectInUnitOfWork(ObjectBuilder.java:513)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObject(ObjectBuilder.java:461)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObject(ObjectBuilder.java:413)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.buildOb ject(ObjectLevelReadQuery.java:521)
at
org.eclipse.persistence.queries.ReadAllQuery.registerResultI nUnitOfWork(ReadAllQuery.java:893)
at
org.eclipse.persistence.queries.ReadAllQuery.executeObjectLe velReadQuery(ReadAllQuery.java:486)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.execute DatabaseQuery(ObjectLevelReadQuery.java:883)
at
org.eclipse.persistence.queries.DatabaseQuery.execute(Databa seQuery.java:666)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.execute (ObjectLevelReadQuery.java:844)
at
org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAll Query.java:456)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.execute InUnitOfWork(ObjectLevelReadQuery.java:906)
at
org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.int ernalExecuteQuery(UnitOfWorkImpl.java:2588)
at
org.eclipse.persistence.internal.sessions.AbstractSession.ex ecuteQuery(AbstractSession.java:1178)
at
org.eclipse.persistence.internal.sessions.AbstractSession.ex ecuteQuery(AbstractSession.java:1162)
at
org.eclipse.persistence.internal.sessions.AbstractSession.ex ecuteQuery(AbstractSession.java:1136)
at
org.eclipse.persistence.internal.sessions.AbstractSession.ex ecuteQuery(AbstractSession.java:1118)
at
org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeRea dQuery(EJBQueryImpl.java:399)
at
org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultL ist(EJBQueryImpl.java:517)

The exception apart, the SQL statement should use left outer joins,
shouldn't it?

-- Frank
Re: Dynamic fetch plan with navigations [message #379041 is a reply to message #379039] Mon, 11 August 2008 15:55 Go to previous messageGo to next message
Frank Schwarz is currently offline Frank SchwarzFriend
Messages: 26
Registered: July 2009
Location: Dresden
Junior Member
Obviously, EclipseLink is jumping over the first relationship.

Doing

Query query = entityManager.createQuery("SELECT p FROM Person p");
query.setHint("eclipselink.join-fetch", "p.friends");
query.setHint("eclipselink.join-fetch", "p.friends.friends");

will prevent the exception.

Is there also a hint to force outer-joins?

-- Frank
Re: Dynamic fetch plan with navigations [message #379264 is a reply to message #379041] Tue, 12 August 2008 13:23 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
There is no hint for using an outerjoin currently, please log a bug for
this.

You can configure this directly on the EclipseLink query
(ObjectLevelReadQuery.addJoinedAttribute(Expression), the Expression will
use either getAllowingNull() or anyOfAllowingNone().

-- James
Re: Dynamic fetch plan with navigations [message #379275 is a reply to message #379264] Thu, 14 August 2008 06:56 Go to previous message
Frank Schwarz is currently offline Frank SchwarzFriend
Messages: 26
Registered: July 2009
Location: Dresden
Junior Member
James wrote:

> There is no hint for using an outerjoin currently, please log a bug for
> this.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=244124

-- Frank
Previous Topic:Eclipselink and Spring
Next Topic:EclipseLink and Firebird
Goto Forum:
  


Current Time: Fri Apr 19 21:59:36 GMT 2024

Powered by FUDForum. Page generated in 0.04102 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top