当前位置: 首页 > 知识库问答 >
问题:

Java JPA Hibernate-错误:无限递归

边意
2023-03-14

产品类别:

@Entity
@Table(name = "products") 
public class Product implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private int id;

@Column(name = "product_category")
private String productCategory;

@Column(name = "product_subcategory")
private String productSubcategory;

@Column(name = "product_name")
private String productName;

@Column(name = "product_description")
private String productDescription;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product", cascade=CascadeType.ALL)
private Set<Prices_1> prices_1 = new HashSet<Prices_1>(0);


public Set<Prices_1> getPrices_1(){
    return prices_1;
}

public void setPrices_1(Set<Prices_1> prices_1){
    this.prices_1 = prices_1;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getProductCategory() {
    return productCategory;
}

public void setProductCategory(String productCategory) {
    this.productCategory = productCategory;
}

public String getProductSubcategory() {
    return productSubcategory;
}

public void setProductSubcategory(String productSubcategory) {
    this.productSubcategory = productSubcategory;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public String getProductDescription() {
    return productDescription;
}

public void setProductDescription(String productDescription) {
    this.productDescription = productDescription;
}

@Override
public String toString() {
    return "ProductController [id=" + id + ", productName=" + productName
            + ", productCategory=" + productCategory + ", productSubcategory=" + productSubcategory
            + ", productDescription=" + productDescription
            + "]";
}

}

提供程序类:

@Entity
@Table(name="providers")
public class Provider implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="provider_id")
private int id;

@Column(name="provider_name")
private String providerName;

@Column(name="provider_address")
private String providerAddress;

@Column(name="provider_phone")
private String providerPhone;

@Column(name="provider_archived")
private int providerArchived = 0;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.provider")
private Set<Prices_1> prices_1 = new HashSet<Prices_1>(0);

public Set<Prices_1> getPrices_1() {
    return prices_1;
}

public void setPrices_1(Set<Prices_1> prices_1) {
    this.prices_1 = prices_1;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getProviderName() {
    return providerName;
}

public void setProviderName(String providerName) {
    this.providerName = providerName;
}

public String getProviderAddress() {
    return providerAddress;
}

public void setProviderAddress(String providerAddress) {
    this.providerAddress = providerAddress;
}

public String getProviderPhone() {
    return providerPhone;
}

public void setProviderPhone(String providerPhone) {
    this.providerPhone = providerPhone;
}

public int getProviderArchived() {
    return providerArchived;
}

public void setProviderArchived(int providerArchived) {
    this.providerArchived = providerArchived;
}

@Override
public String toString() {
    return "ProviderController [id=" + id + ", providerName=" + providerName
            + ", providerAddress=" + providerAddress + ", providerPhone="
            + providerPhone + "]";
}

}

Prices_1类:

@Entity
@Table(name = "prices_1")
@AssociationOverrides({
    @AssociationOverride(name = "pk.product",
            joinColumns = @JoinColumn(name = "product_id")),
    @AssociationOverride(name = "pk.provider",
            joinColumns = @JoinColumn(name = "provider_id"))})
public class Prices_1 implements java.io.Serializable {

@EmbeddedId
private PricesId pk = new PricesId();

@Column(name = "price")
private float price;

public PricesId getPk() {
    return pk;
}

public void setPk(PricesId pk) {
    this.pk = pk;
}

public float getPrice() {
    return price;
}

public void setPrice(float price) {
    this.price = price;
}

@Transient
public Product getProduct() {
    return getPk().getProduct();
}

public void setProduct(Product product) {
    getPk().setProduct(product);
}

@Transient
public Provider getProvider() {
    return getPk().getProvider();
}

public void setProvider(Provider provider) {
    getPk().setProvider(provider);
}


public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;

    Prices_1 that = (Prices_1) o;

    if (getPk() != null ? !getPk().equals(that.getPk())
            : that.getPk() != null)
        return false;

    return true;
}

public int hashCode() {
    return (getPk() != null ? getPk().hashCode() : 0);
}

}
@Embeddable
public class PricesId implements java.io.Serializable {

@ManyToOne
private Product product;

@ManyToOne
private Provider provider;

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public Provider getProvider() {
    return provider;
}

public void setProvider(Provider provider) {
    this.provider = provider;
}

public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    PricesId that = (PricesId) o;

    if (product != null ? !product.equals(that.product) : that.product != null) return false;
    if (provider != null ? !provider.equals(that.provider) : that.provider != null) return false;

    return true;
}

public int hashCode() {
    int result;
    result = (product != null ? product.hashCode() : 0);
    result = 31 * result + (provider != null ? provider.hashCode() : 0);
    return result;
}

}
@Repository
public class ProductsRepository {

@PersistenceContext
EntityManager em;

public List<Product> findAllProducts(){
    Query query = em.createQuery("SELECT e FROM Product e");
    return (List<Product>) query.getResultList();
}

}
@Service
public class ProductsService {

@Autowired
ProductsRepository productsRepository;

@Transactional(readOnly = true)
public List<Product> findAllProducts(){
    List<Product> pv = productsRepository.findAllProducts();
    return pv;
}

}
@Controller
@RequestMapping("product")
public class ProductController {

@Autowired
private ProductsService productsService;

@ResponseBody
@ResponseStatus(HttpStatus.OK)
@RequestMapping(method = RequestMethod.GET)
public List<Product> getProducts(){
    List<Product> productList = productsService.findAllProducts();
    for (Product p : productList){
        System.out.println("Andres. Provider name: " + p.getProductName());
    }
    return productList;
}
}

共有1个答案

朱华皓
2023-03-14
public class GenericClass<E> implements List<E> {

private List<E> list;
public GenericClass() {
    this.list = new ArrayList<>();
}


@Override
public String toString() {
    StringBuilder s = new StringBuilder();
    for (E e : list) {
        s.append(e.toString());
    }
    return s.toString();
}

@Override
public boolean add(E e) {

    return list.add(e);
}

@Override
public boolean addAll(Collection<? extends E> c) {

    return list.addAll(c);
}

}

 类似资料:
  • 问题内容: 丈夫.java 妻子.java Service.java 当我使用spring数据jpa从数据库中查询丈夫时,结果发生无限递归,参见下图。使用@OneToOne注释时出了什么问题?有人可以给我一些建议吗?或者我以错误的方式使用了注释。 图片 问题答案: 这是一个已知问题,当您具有双向关系时,杰克逊将尝试从另一侧序列化一侧的每个引用,以便逻辑上具有无限递归。 解决方案:有很多解决方案,可

  • 问题内容: 我想覆盖对类中一个变量的访问,但通常返回所有其他变量。我该怎么做呢? 我尝试了以下操作(它也应说明我要执行的操作),但是出现了递归错误: 问题答案: 您收到递归错误,因为您尝试访问其中的属性会再次调用您。如果你使用的不是,它的工作原理: 之所以有效,是因为(在此示例中)是基类。通过调用您的基本版本,可以避免您以前遇到的递归地狱。 IPython的输出与foo.py中的代码: 更新: 在

  • 在尝试执行GET到发布者存储库时,我正在执行GET和无限循环。 出版商: 书: 完整代码可在此处获得: https://github.com/vanyasav/library

  • 我正在做一个EclipseLink项目,在这个项目中,一个用户可以“跟踪”另一个用户,就像在社交媒体网站上一样。我使用实体(引用了一个名为的表)进行了设置,该实体具有一个“followers”(跟随该用户的用户)列表和另一个“followed”(该用户正在跟随的用户)列表。该关系在一个名为的单独表中定义,该表包含以下用户的ID()和以下用户的ID()的列。 我的用户模型如下所示: 方法似乎工作得很

  • 我用的是Spring Roo 1.2.1和Jackson 1.9.7。在使用json序列化我的类时,我得到了一个JsonMappingException。 我读了以下帖子,但没有找到适合我的工作解决方案: Jackson的无限递归 Jackson-具有双方向关系的实体序列化(避免循环) 我不知道为什么JsonIgnore在属性QueueOuts的类Queue中不起作用。我也尝试了JsonManag

  • 请不要标记重复,只需更正我的许可,谢谢