mysql - 当尝试从链接到另一个实体的用户那里获取详细信息时,它会给出 StackOverflow 错误
问题描述
我正在尝试学习休眠,因为我正在尝试制作一个应用程序,用户可以在其中添加一些产品,当我们获取用户并调用产品列表时,它应该获取该特定用户添加的产品我有定义了用户和产品之间的@OneToMany 映射,但是当我试图获取与他有一些产品的用户时,它给了我StackOverflow
错误,但是当我打电话给没有产品的用户时,它工作正常。这是我的代码,请看一下,请让我知道我在这里做错了什么。
用户模型.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 + "\"}";
}
}
产品模型.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 + "\", \"name\":\"" + name + "\", \"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 错误。
推荐阅读
- amazon-web-services - 如何在 Amazon AWS S3 存储桶中找到未加密的文件?
- sql - 如何从视图列中获取 SQLite 列类型
- json - 解析 JSON 文件而无需将其保存在文件中
- python - 使用 GPIO.RaspberryPi3 上的触发器发送电子邮件
- django - django allauth 设置 EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL 不起作用
- asp.net - 使用 Ninject 进行 Asp.Net Webforms 构造函数注入
- javascript - 如何使用 Javascript 访问带有 innerHTML 的 twig 变量?
- javascript - 焦点选择的 vue.js 元素不是反应式的
- excel - 如何使用每个循环将信息传输到另一个工作表
- html - amp-html:在 amp-list src 中使用查询参数