【问题标题】:JPA Insert Into Large ManyToManyJPA 插入大型多对多
【发布时间】:2020-02-07 14:18:03
【问题描述】:

我有这 2 个实体(商店和产品)我已经有了产品,我想将其分配给商店,问题是商店可能有数千种产品,我必须“插入”将产品存储在内存中,然后将存储与产品集一起保存。这让我有一个 StackOverflowError 并且效率非常低。

有没有办法做这样的事情?

insert into product_store (product_id, store_id) VALUES (:productIds, :storeId)

实体:

@Entity
@Table(name = "stores")
data class Store(@Id
                 @GeneratedValue(strategy = GenerationType.IDENTITY)
                 val id: Long = 0,
                 @Column
                 val name: String,
                 @Column
                 val street: String,
                 @Column
                 val streetNumber: String,
                 @Column
                 val postalCode: Int,
                 @Column
                 val city: String,
                 @Column
                 val state: String,
                 @Column
                 val latitude: Double,
                 @Column
                 val longitude: Double,
                 @ManyToMany(fetch = FetchType.LAZY)
                 @JoinTable(name = "product_store",
                         joinColumns = [JoinColumn(name = "store_id")],
                         inverseJoinColumns = [JoinColumn(name = "product_id")])
                 val products: Set<Product>

) : DateAudit()

产品实体:

@Entity
@Table(name = "products")
data class Product(@Id
                   @GeneratedValue(strategy = GenerationType.IDENTITY)
                   val id: Long = 0,
                   @Column(nullable = false)
                   val codeBar: String,
                   @Column(nullable = false)
                   val name: String,
                   @Column(columnDefinition = "text")
                   val description: String,
                   @Column(columnDefinition = "text")
                   val ingredients: String?,
                   val picture: String,
                   @OneToMany(fetch = FetchType.EAGER, cascade = [CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH])
                   val nutriments: Set<Nutriment>,
                   @ManyToOne(fetch = FetchType.EAGER)
                   @JoinColumn(name = "brand_id")
                   val brand: Brand,
                   @ManyToMany(fetch = FetchType.EAGER, cascade = [CascadeType.MERGE])
                   @JoinTable(name = "product_category",
                           joinColumns = [JoinColumn(name = "product_id")],
                           inverseJoinColumns = [JoinColumn(name = "category_id")])
                   val categories: Set<Category>
) : DateAudit()

【问题讨论】:

    标签: spring hibernate spring-boot jpa


    【解决方案1】:

    这样固定

    @Modifying
        @Query("INSERT INTO product_store (product_id, store_id) " +
                "(SELECT p.id, s.id FROM products p, stores s WHERE p.market_id IN (:productMarketIds) AND s.id = :storeId)", nativeQuery = true)
        fun saveProductsToStoreByProductMarketIds(storeId: Long, productMarketIds: Set<String>)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      相关资源
      最近更新 更多