我在Spring JPA中创建了一个@ManyToMany的示例,其中包含两个实体Category和Product,实体如下所示
www.example.com
import jakarta.persistence.*;
import lombok.*;
import java.util.Objects;
import java.util.Set;
import org.hibernate.Hibernate;
@Table(name = "Product")
@Entity(name = "Product")
@Builder
@RequiredArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column
private String description;
@Column
private double price;
@Column
private Integer quantity;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "product_category",
joinColumns = @JoinColumn(name = "product_id"),
inverseJoinColumns = @JoinColumn(name = "category_id")
)
private Set<Category> categories;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Product product = (Product) o;
return id != null && Objects.equals(id, product.id);
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
www.example.com
import jakarta.persistence.*;
import lombok.*;
import java.util.Objects;
import java.util.Set;
import org.hibernate.Hibernate;
@Table(name = "Category")
@Entity(name = "Category")
@Builder
@RequiredArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String name;
@ManyToMany(mappedBy = "categories" , fetch = FetchType.EAGER)
private Set<Product> products;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Category category = (Category) o;
return id != null && Objects.equals(id, category.id);
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
当我尝试使用findAll()
从CategoryRepo
获取所有类别时,它会给我StackOverFlow
异常
at java.base/java.lang.String.valueOf(String.java:4216) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173) ~[na:na]
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
at org.hibernate.collection.spi.PersistentSet.toString(PersistentSet.java:296) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453) ~[na:na]
at com.app.ecommerce.entity.Category.toString(Category.java:48) ~[classes/:na]
at java.base/java.lang.String.valueOf(String.java:4216) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173) ~[na:na]
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
at org.hibernate.collection.spi.PersistentSet.toString(PersistentSet.java:296) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453) ~[na:na]
at com.app.ecommerce.entity.Product.toString(Product.java:63) ~[classes/:na]
at java.base/java.lang.String.valueOf(String.java:4216) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173) ~[na:na]
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
at org.hibernate.collection.spi.PersistentSet.toString(PersistentSet.java:296) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453) ~[na:na]
at com.app.ecommerce.entity.Category.toString(Category.java:48) ~[classes/:na]
at java.base/java.lang.String.valueOf(String.java:4216) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173) ~[na:na]
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
at org.hibernate.collection.spi.PersistentSet.toString(PersistentSet.java:296) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453) ~[na:na]
at com.app.ecommerce.entity.Product.toString(Product.java:63) ~[classes/:na]
at java.base/java.lang.String.valueOf(String.java:4216) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173) ~[na:na]
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
at org.hibernate.collection.spi.PersistentSet.toString(PersistentSet.java:296) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453) ~[na:na]
at com.app.ecommerce.entity.Category.toString(Category.java:48) ~[classes/:na]
at java.base/java.lang.String.valueOf(String.java:4216) ~[na:na]
我也试着使用@JsonIgnore
和@JsonIgnorePropertie
,但它不工作。
我想返回所有类别如下
[
{
"id" : 1,
"name" : "Mobile"
}
]
1条答案
按热度按时间vulvrdjw1#
无限递归是
Product
和Category
的toString
方法之间的。至少在一个排除另一个,由@ToString
注解@ToString.Exclude
toString
方法。