hibernate - @EntityGraph 获取实体内部的实体
问题描述
我正在尝试使用 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 注释?
解决方案
推荐阅读
- sql-server-2008-r2 - SQL Server左连接未从左表返回预期记录
- c# - 在 BULK INSERT 时触发更新触发器后
- java - PrimeFaces dataTable 在使用标签更新的异步部分提交请求后未更新?
- sapui5 - 如何使列表项可重新排序?
- xml - Odoo 11. 从 xml 导入 many2one
- sql - 如何将 varchar 转换为 hh:mm:ss 的日期时间格式?
- c# - #c Wpf 数据绑定
- javascript - 确定大写和小写与代码块的比较
- stripe-payments - 创建 Stripe 3ds 源时是否可以获取卡片信息?
- rest - Rest API 封装与否