Spring JPA插入到大型多个
我有这两个实体商店和产品我已经有了产品,我想把它分配给商店,问题是一个商店可能有数千种产品,要插入,我必须把产品放在内存中,然后用产品集保存商店。这使我有一个堆栈溢出错误,而且效率非常低 有没有办法做到这一点Spring JPA插入到大型多个,spring,hibernate,spring-boot,jpa,Spring,Hibernate,Spring Boot,Jpa,我有这两个实体商店和产品我已经有了产品,我想把它分配给商店,问题是一个商店可能有数千种产品,要插入,我必须把产品放在内存中,然后用产品集保存商店。这使我有一个堆栈溢出错误,而且效率非常低 有没有办法做到这一点 insert into product_store (product_id, store_id) VALUES (:productIds, :storeId) 实体: @Entity @Table(name = "stores") data class Store(@Id
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()
像这样修理
@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>)