'Spring JPA : Exclude lob field from child in EntityGraph

I have an entity called application which have an icon to be loaded : My main problem is how to exclude some field that are used in the entityGraph

 @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Table
    @Entity
    @Indexed
    @NamedEntityGraph(name = "Application.list",
            attributeNodes = {
                    @NamedAttributeNode("icon"),
                    @NamedAttributeNode("category")
            }
    )
    public class Application {
    
        @Id
        @Column
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
    
        @ManyToOne(fetch = FetchType.EAGER)
        private Category category;
    
        @ManyToOne(fetch = FetchType.EAGER)
        private ApplicationType type;
    
        @ManyToOne(fetch = FetchType.EAGER)
        private Category category;
    
        @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        private Icon icon;
    
      
    }

the icon child class :


@Table
@Entity
public class Icon {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String label;

    @Column
    private String type;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String image;

}

when i call this :

@EntityGraph(value = "Application.list")
List<Application> findByCategoryId(Long categoryId, Sort sort);

the query result is :

select
        applicatio0_.id as id1_3_0_,
        category1_.id as id1_18_1_,
        icon2_.id as id1_24_2_,
        applicatio3_.id as id1_9_3_,
        applicatio0_.category_id as category_id7_3_0_,
        applicatio0_.deleted as deleted2_3_0_,
        applicatio0_.description as description3_3_0_,
        applicatio0_.guide as guide4_3_0_,
        applicatio0_.icon_id as icon_id8_3_0_,
        applicatio0_.link as link5_3_0_,
        applicatio0_.name as name6_3_0_,
        applicatio0_.type_id as type_id9_3_0_,
        category1_.name as name2_18_1_,
        icon2_.label as label2_24_2_,
        icon2_.type as type3_24_2_,2
        ***icon2_.image as type3_24_2_,***
        applicatio3_.name as name2_9_3_ 
    from
        application applicatio0_ 
    left outer join
        category category1_ 
            on applicatio0_.category_id=category1_.id 
    left outer join
        icon icon2_ 
            on applicatio0_.icon_id=icon2_.id 
    left outer join
        application_type applicatio3_ 
            on applicatio0_.type_id=applicatio3_.id 
    where
        category1_.id=? 
    order by
        lower(applicatio0_.name) asc

My question is how to exclude the child field icon2_.image as type3_24_2_, from the query findByCategoryId cause this one will load a big data, so i want that will be a LAzy field.



Solution 1:[1]

@EntityGraph(value = "Application.list2" type = EntityGraph.EntityGraphType.FETCH)
List<Application> findByCategoryId(Long categoryId, Sort sort);

@NamedEntityGraph(name = "Application.list2",
attributeNodes = {@NamedAttributeNode("category")})
public class Application {}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dpassyann