1

我正在尝试使用 EntityGraph 获取所有产品翻译。它工作正常,但它也获取所有延迟获取的实体。我的产品实体:

@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

ProductTranslate新实体:

@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

我的仓库:

@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);
}

当我调用 getAllProductBySupplierId 方法时,我会在 Translate 中获取 Product。它也获取产品。
有没有办法不获取 Entity - 用 FetchType.Lazy 注释?

4

0 回答 0