gpt4 book ai didi

java - 为什么无限循环在加载数据时 hibernate

转载 作者:行者123 更新时间:2023-11-30 10:02:30 25 4
gpt4 key购买 nike

我尝试加载 storebyid 但在删除时显示错误

@ManyToMany(mappedBy = "stores")
private Set<Product> products;

我可以加载storebyid。

循环 hibernate :

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;
}
}

最佳答案

我认为这可能与延迟加载的集合有关 - 默认情况下,@ManyToMany 注释获取类型是 FetchType.LAZY。尝试将其更改为 FetchType.EAGER。也可以尝试设置 CascadeType.MERGE

仅供引用,当您使用 @Data 时,您不必显式添加 getter 和 setter。

关于java - 为什么无限循环在加载数据时 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com