views:

644

answers:

2

Hi guys,

With Hibernate I'm used to do something like the following:

select n from NetworkElement n join fetch n.site s where s.active is true

However, EclipseLink complains a lot about this:

Caused by: Exception [EclipseLink-8024] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing the query [select n from NetworkElement n join fetch n.site s], line 1, column 49: syntax error at [s].

(The query on the stack is different from the one above, but the result is the same)

I've tried different combinations, none of which worked:

select n from NetworkElement n join fetch n.site where n.site.active is true
select n from NetworkElement n join fetch n.site as site where site.active is true

I also tried switching to a different entity in my domain model, suspecting that maybe my mapping is not correct. Still, the same problem.

Could it be that I can only achieve this using a query hint? I don't want to do that.

By the way, I'm using EcliseLink as shipped with Netbeans 6.8 and Glassfish v3.

I'd appreciate any help!

Rodrigo

+2  A: 

The main issue is that the JPQL syntax does not allow for aliasing fetch joins and that is why EclipseLink uses query hints for this functionality. There is an Enhancement Request to add aliasing join fetches directly in the JPQL and if you would like to see it completed please vote for it. ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=293775 ).

--Gordon Yorke

Gordon Yorke
Are you sure? Hibernate allows me to do it through JPA. Well, it could be a Hibernate extension, but I always though this was a native JPQL feature.
javabeats
A: 

Well, it seems that one is not allowed to alias a fetch join in JPQL, indeed.. It works with Hibernate because it supports aliasing through HQL, and you are allowed to issue HQL to a JPA Query object.

Therefore, I had no choice but to switch to named queries with query hints. I don't really like declaring queries with annotations because of the high verbosity on the entity classes, so I added a orm.xml file to the persistence unit's jar, and did the following:

<!-- Fetch network elements -->
<named-query name="fetchNetworkElements">
    <query>select n from NetworkElement n</query>
    <lock-mode>NONE</lock-mode>
    <hint name="eclipselink.join-fetch" value="n.site" />
    <hint name="eclipselink.join-fetch" value="n.site.area" />
</named-query>

Hope this gives some clue to anybody struggling with the same shortcomings of the raw JPQL.

javabeats