【问题标题】:Could performance issue occure for manyTomany?manyTomany 会出现性能问题吗?
【发布时间】:2018-09-29 20:05:45
【问题描述】:

我有这样的代码

   public Serializable save(Product product) {
    product.getCategories().forEach(category -> category.getProducts().add(product));

    return super.save(product);
   }

类别会有很多产品,hibernate会全部加载吗?

如果是,如何在 dao 或实体级别解决此问题?

@Table//...
public class Product {//...
@Cascade(value = SAVE_UPDATE)
@ManyToMany(mappedBy = "products", fetch = FetchType.LAZY, targetEntity = Category.class)
private Set<Category> categories = new HashSet<>();

}

@Table//...
public class Category {//... 
@Cascade(value = SAVE_UPDATE)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "productToCategory",
        joinColumns = {@JoinColumn(name = "categoryId")},
        inverseJoinColumns = {@JoinColumn(name = "productId")})
private Set<Product> products = new HashSet<>();
}

【问题讨论】:

    标签: java sql database hibernate orm


    【解决方案1】:

    这是我的问题的答案:

    post

    【讨论】:

      猜你喜欢
      • 2017-01-15
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 2019-11-11
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      相关资源
      最近更新 更多