当尝试从链接到另一个实体的用户获取详细信息时,它给出了StackOverflow错误

问题描述

我正在尝试学习休眠方式,因为我正在尝试制作一个用户可以在其中添加一些产品的应用程序,并且当我们获取用户并调用产品列表时,它应该获取该特定用户添加的产品以用于我已经定义了用户和产品之间的@OneToMany映射,但是当我尝试获取与他一起有一些产品的用户时,会给我StackOverflow错误,但是当我致电没有产品的用户时,它可以正常工作。这是我的代码,请看一下,并让我知道我在这里做错了。

UserModel.java

package com.solitera.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class UserModel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="user_name")
    private String name;
    @Column(name="user_password")
    private String password;
    @Column(name="user_email")
    private String email;
    @Column(name="user_address")
    private String address;
    @Column(name="user_phone")
    private String phoneNumber;
    @Column(name="user_role")
    private Integer role;

    @OneToMany(mappedBy = "addedBy",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
//  @JoinColumn(name="added_by")
    private List<ProductModel> products;
    
        
    public UserModel() {
    }


    public UserModel(String name,String password,String email,String address,String phoneNumber,Integer role) {
        this.name = name;
        this.password = password;
        this.email = email;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.role = role;
    }


    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Integer getRole() {
        return role;
    }
    public void setRole(Integer role) {
        this.role = role;
    }


    public List<ProductModel> getProducts() {
        return products;
    }
    public void setProducts(List<ProductModel> products) {
        this.products = products;
    }


    @Override
    public String toString() {
        return "{\"id\":\"" + id + "\",\"name\":\"" + name + "\",\"password\":\"" + password + "\",\"email\":\""
                + email + "\",\"address\":\"" + address + "\",\"phoneNumber\":\"" + phoneNumber + "\",\"role\":\""
                + role + "\",\"products\":\"" + products + "\"}";
    }   
    
}

ProductModel.java

package com.solitera.model;

import java.sql.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="products")
public class ProductModel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="name")
    private String name; 

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name="added_by")
    private UserModel addedBy;

    @Column(name="price")
    private Integer price;
    
    @OneToMany(mappedBy = "productid",cascade = CascadeType.ALL)
    private List<PictureModel> pictures;
    
    public ProductModel() {
    }


    public ProductModel(String name,UserModel addedBy,Integer price) {
        this.name = name;
        this.addedBy = addedBy;
        this.price = price;
    }


    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public UserModel getAddedBy() {
        return addedBy;
    }
    public void setAddedBy(UserModel addedBy) {
        this.addedBy = addedBy;
    }

    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }

    public List<PictureModel> getPictures() {
        return pictures;
    }
    public void setPictures(List<PictureModel> pictures) {
        this.pictures = pictures;
    }


    @Override
    public String toString() {
        return "{\"id\":\"" + id + "\",\"addedBy\":\"" + addedBy + "\",\"price\":\""
                + price + "\",\"pictures\":\"" + pictures + "\"}";
    }



    
}

UserServiceImpl.java

 public UserModel getuser(UserModel userModel,EntityManager entityManager) {
     Boolean isIntMgr = false;
     
     if(entityManager == null) {
         entityManager = MyEntityManagerFactory.getEntityManagerFactory().createEntityManager();
         entityManager.getTransaction().begin();
         isIntMgr =true;
     }
     Integer id= 8;
     
     userModel = entityManager.find(UserModel.class,id);
     
     logger.info("The user Model for USer id 8 is : "+ userModel );
     logger.info("This are the products : " + userModel.getProducts());
//   for(ProductModel products : userModel.getProducts()) {
//       logger.info("This are the products : " + products);
//   }  
     if(isIntMgr) {
         entityManager.getTransaction().commit();
     }
     return userModel;
 }

控制台

04:09:2020 00:02:38 [LogHelper] HHH000204: Processing PersistenceUnitInfo [
    name: my_test_eCommerce
    ...]
04:09:2020 00:02:38 [Version] HHH000412: Hibernate Core {5.3.2.Final}
04:09:2020 00:02:38 [Environment] HHH000206: hibernate.properties not found
04:09:2020 00:02:39 [Version] HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
04:09:2020 00:02:39 [Dialect] HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
Hibernate: 
    select
        usermodel0_.id as id1_2_0_,usermodel0_.user_address as user_add2_2_0_,usermodel0_.user_email as user_ema3_2_0_,usermodel0_.user_name as user_nam4_2_0_,usermodel0_.user_password as user_pas5_2_0_,usermodel0_.user_phone as user_pho6_2_0_,usermodel0_.user_role as user_rol7_2_0_ 
    from
        user usermodel0_ 
    where
        usermodel0_.id=?
Hibernate: 
    select
        products0_.added_by as added_by4_1_0_,products0_.id as id1_1_0_,products0_.id as id1_1_1_,products0_.added_by as added_by4_1_1_,products0_.name as name2_1_1_,products0_.price as price3_1_1_ 
    from
        products products0_ 
    where
        products0_.added_by=?
04:09:2020 00:02:40 [UserServiceImpl] --------------------Finally in Main ---------------------
Exception in thread "main" java.lang.StackOverflowError
    at java.base/java.lang.Integer.toString(Integer.java:441)
    at java.base/java.lang.Integer.toString(Integer.java:1165)
    at java.base/java.lang.String.valueOf(String.java:2801)
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:135)
    at com.solitera.model.ProductModel.toString(ProductModel.java:88)
    at java.base/java.lang.String.valueOf(String.java:2801)
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:135)
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:473)

解决方法

您应该从products中删除UserModel.toString()

    @Override
    public String toString() {
        return "{\"id\":\"" + id + "\",\"name\":\"" + name + "\",\"password\":\"" + password + "\",\"email\":\""
                + email + "\",\"address\":\"" + address + "\",\"phoneNumber\":\"" + phoneNumber + "\",\"role\":\""
                + role + "\"}";
    } 

由于用户的toString调用产品的toString等,因此出现了stackoverflow错误。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...