Join multiple entities using the where clause and get the result from this.

I have tiered objects configured and successfully matched, and everything works as expected.

Now I want to use a custom query in which I join several entities and define some where clauses are.

public interface GiataItemRepository extends JpaRepository<GiataItem, String>{      
    @Query(value="select g from GiataItem g "
            + "join g.service s "
            + "join s.section se "
            + "join se.secPic sp "
            + "where g.giataId = :giataId "
            + "and se.secScope = 'MAINGALLERY' "
            + "and sp.category = 'MAINGALLERY' "
            + "and sp.seqOrder = 0")
    GiataItem findPicture(@Param("giataId") String giataId);
}

SQL gives me the correct result for mine GiataItem. But I have no restrictions on my where clauses for all other mapped objects, such as service, sectionetc.

I use lazy loading, and its clarity, when I use giataIetem.getService, JPA makes a new choice, and my suggestions don't work there.

So, how can I make sure that all of my combined objects are built on where clauses and its limitations.

+4
1

, JOIN FETCH:

@Query(value="SELECT g FROM GiataItem g "
        + "JOIN FETCH g.service as s "
        + "JOIN FETCH s.section as se "
        + "JOIN FETCH se.secPic as sp "
        + "WHERE g.giataId = :giataId "
        + "AND se.secScope = 'MAINGALLERY' "
        + "AND sp.category = 'MAINGALLERY' "
        + "AND sp.seqOrder = 0")

, :

FetchMode Spring Data JPA

+2

Source: https://habr.com/ru/post/1684284/


All Articles