Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to do a complex LEFT JOIN FETCH condition in JPQL?(How to do a complex LEFT JOIN FETCH condition in JPQL?)
How to do a complex LEFT JOIN FETCH condition in JPQL? [message #1717235] Thu, 10 December 2015 21:56 Go to next message
Estevao Jordao is currently offline Estevao JordaoFriend
Messages: 3
Registered: December 2015
Junior Member
I'm a very simple question that I'm not able to solve, how can I make a named Query using left join fetch to load only the children who are in a condition?

I'm using eclipselink (JPQL) and JPA 2.x:

This is my named Query:

    SELECT step
    FROM StepEntity step
    left join fetch step.precedentsSteps precedentStep 
        ON precedentStep.isCondition = :isCondition
        and precedentStep.id.stepCode = :stepCode
        and precedentStep.id.country = :country 
        and precedentStep.id.stageCode = :stageCode  
    WHERE step.id.stepCode = :stepCode
    AND step.id.country = :country 
    and step.id.stageCode = :stageCode



step.precedentsSteps is a @OneToMany(targetEntity=PrecedentStepEntity.class, fetch=FetchType.LAZY, mappedBy = "step")

When I use the left join fetch it is bringing all the children that are in the Step entity and I just want it load the collection on the condition = true;

I am very grateful for the help already
Re: How to do a complex LEFT JOIN FETCH condition in JPQL? [message #1717646 is a reply to message #1717235] Tue, 15 December 2015 14:23 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1380
Registered: July 2009
Senior Member
Queries are meant to return managed objects that reflect exactly what is in the database, and allows for different layers of caching that would be affected if queries brought in different views of an entity. As such, you can't build a query to return managed entity instance that are incomplete, at least not directly. If this is required, I would normally recommend you use a separate query for the relationship that you filter as needed rather then include it in the entity. Or add a getter method that filters the collection so that the full collection is maintained, but your application has access to the partial collection where isCondition is true or false.

What you are asking for is refered to as mapping selection criteria in Eclipselink (https://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria) but it isn't usually dynamic. It can probably be made dynamic with redirector queries as suggested here http://stackoverflow.com/questions/15967541/eclipselink-jpa-mappingselectioncriteria-costumization though I haven't seen it done.

Best Regards,
Chris

Previous Topic:MongoDB and multi-tenancy
Next Topic:MOXY JSON Serialization of Polymorphic List issue
Goto Forum:
  


Current Time: Wed Mar 03 09:33:47 GMT 2021

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

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

Back to the top