1

I am trying to fetch all Product Translates using EntityGraph. It works properly but it fetch all Lazy Fetched Entities also. My Product Entity:

@Entity
@Table(name = "product")
@Where(clause = "status = 'ACTIVE'")
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;

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

...

    @OneToMany(mappedBy = "product")
    private Set<ProductTranslateNew> translates = new HashSet<>();

    //Getters & Setters

ProductTranslateNew Entity:

@Entity
@Table(name = "product_translate_new")
public class ProductTranslateNew implements Serializable {

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

    @Column(name = "product_name")
    private String productName;

    ...

    @Basic
    @Column(name = "language")
    private String language;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties("translates")
    private Product product;

    //Getters & Setters

My Repository:

@Repository
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {

@EntityGraph(attributePaths = {"images", "translates"})
    @Query(value = "SELECT p " +
        "FROM Product p left join p.translates t " +
        "WHERE p.supplier.id = :suppId AND t.language = :lang",
        countQuery = "SELECT count(p) " +
            "FROM Product p left join p.translates t " +
            "WHERE p.supplier.id = :suppId AND t.language = :lang")
    Page<Product> getAllProductBySupplierId(@Param("suppId") Long id,
                                               @Param("lang") String language,
                                               Pageable pageable);
}

When I call getAllProductBySupplierId method I get Product inside Translate. It fetch Product also.
Is there any way don't fetch Entity - annotated with FetchType.Lazy ?

pronomy
  • 204
  • 4
  • 15
  • Because its already fetched as the starting point (the 'two' Product are the same). Add a whole different entity relation into your ProductTranslateNew and checkit ...it wont be fetched. – beatrice Nov 08 '19 at 23:11

0 Answers0