我尝试加载storebyid,但在删除@ManyToMany(mappedBy =“ stores”)时显示错误 私人套装产品 我可以加载storebyid
循环休眠
Hibernate: select store0_.store_id as store_id1_6_0_, store0_.status as status2_6_0_, store0_.storename as storenam3_6_0_ from store store0_ where store0_.store_id=?
Hibernate: select products0_.store_id as store_id2_4_0_, products0_.product_id as product_1_4_0_, product1_.product_id as product_1_3_1_, product1_.price as price2_3_1_, product1_.productname as productn3_3_1_ from product_store products0_ inner join product product1_ on products0_.product_id=product1_.product_id where products0_.store_id=?
2562-07-05 16:02:01.197 WARN 15200 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.gpch.hotel.model.Store["products"])
2562-07-05 16:02:01.198 WARN 15200 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.gpch.hotel.model.Store["products"])
2562-07-05 16:02:01.198 WARN 15200 --- [nio-8080-exec-9] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@876165<rs=HikariProxyResultSet@11556352 wrapping com.mysql.jdbc.JDBC42ResultSet@1095230>
package com.gpch.hotel.model;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
@Data
@Entity
@Table(name = "store")
public class Store {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "store_id")
private int id;
@Column(name = "storename")
private String storeName;
@Column(name = "status")
private String status;
@ManyToMany(mappedBy = "stores")
private Set<Product> products;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
package com.gpch.hotel.model;
import lombok.Data;
import javax.persistence.*;
import java.util.Set;
@Data
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private int id;
@Column(name = "productname")
private String productname;
@Column(name = "price")
private int price;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "product_store", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "store_id"))
private Set<Store> stores;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Set<Store> getStores() {
return stores;
}
public void setStores(Set<Store> stores) {
this.stores = stores;
}
}
答案 0 :(得分:1)
我认为这可能与延迟加载的集合有关-对于@ManyToMany注释,默认获取类型为select
f.fundno,
f.fund_name,
case
when ((max(case when fa.attribute_type in ('RCLASS') then
fa.attribute_type end)) is not null)
then 'Y'
else ' '
end as Reclassification,
max(case when fa.attribute_type in ('RCLASS') then rf.reclass_to_fundno end) as Reclass_To_Fund,
max(case when fa.attribute_type in ('RCLASS') then rf.reclass_months end) as Reclass_Months
from
fund f
join fund_attribute fa
on f.fundno = fa.fundno
left join reclassification_fund rf
on f.fundno = rf.fundno
where f.fund_status_code='A'
and f.category_code in(1,13,16,18,19,20,27,31)
group by
f.fundno,
f.fund_name,
f.fund_type,
f.fund_status_code,
f.category_code
order by f.fund_name
。尝试将其更改为FetchType.LAZY
。另外,尝试设置FetchType.EAGER
。
仅供参考,当您使用CascadeType.MERGE
时,不必显式添加getter和setter。
答案 1 :(得分:0)
更多是关于杰克逊(Jackson)和序列化为实体字符串的错误。如果我错了,请更正我,但我认为您是通过rest服务返回该实体。 Jackson会产生一个stackoverflow错误,因为它在子实体(产品)上找到了到父实体(商店)的链接,因此它陷入了无限循环。
您可以添加一个 @JsonIgnore 在Product实体中的getStores函数上。这将阻止infite循环,并在商店和产品内部添加一个json。