Announcement Announcement Module
No announcement yet.
Eager Fetch within a Specification Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Eager Fetch within a Specification

    Can someone provide an example of how to create an eager fetch predicate within a Specification?

    Let's say you have an entity which has an association to some children of the same entity. The association is a OneToMany.

    How can a Specification be used within a findAll call, to activate an EAGER fetch on the children property?

    public class SomeEntity implements Serializable {

    @SequenceGenerator(name="SOME_THING_SEQUENCE", sequenceName="SOME_THING_SEQUENCE")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SOME_THING_SEQUENCE")
    @Column(name="SOME_THING_ID", unique=true, nullable=false)
    private Integer someThingId;

    @OneToMany(fetch=FetchType.LAZY,cascade = CascadeType.MERGE)
    @JoinColumn(name = "PARENT_ID")
    private Collection<SomeEntity> children = new ArrayList<SomeEntity>();


  • #2
    Use @Query...

    Ended up implementing with a Query in a method on the repository interface:

    @Query("select t from SomeEntity t LEFT join fetch t.children where t.treeNodeId=:treeNodeId")
    List<SomeEntity> findTreeByNodeIdEagerChildren(@Param("treeNodeId") Integer treeNodeId);


    • #3
      agree that there should be a feature in SpringData JPA for that issue.