Hibernate创建两个多对一(多对多)时报错:Exception in thread “main“ java.lang.StackOverflowError

项目场景:

项目相关背景:使用Hibernate框架进行多对多(两个多对一)创建时,运行测试文件发现报错。


问题描述:

代码:

//实体类订单代码
@Entity
@Table(name="orders",catalog="work")
public class Orders implements Serializable{
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer oid;
	private String type;
	
	@OneToMany(targetEntity=Products_Orders.class,mappedBy="o",cascade=CascadeType.ALL)
	private Set<Products_Orders> spo = new HashSet<Products_Orders>();

	public Integer getOid() {
		return oid;
	}

	public void setOid(Integer oid) {
		this.oid = oid;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Set<Products_Orders> getSpo() {
		return spo;
	}

	public void setSpo(Set<Products_Orders> spo) {
		this.spo = spo;
	}

	public Orders( String type) {
		super();
		this.type = type;
	}

	public Orders() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "Orders [oid=" + oid + ", type=" + type +", spo"+spo+ "]";
	}
	
	
}
//实体类商品代码
@Entity
@Table(name="products",catalog="work")
public class Products implements Serializable{
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer pid;
	private String pname;
	private String introduce;
	private double price;
	
	@OneToMany(targetEntity=Products_Orders.class,mappedBy="p",cascade=CascadeType.ALL)
	private Set<Products_Orders> spo = new HashSet<Products_Orders>();

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public double getPrice() {
		return price;
	}

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

	public Set<Products_Orders> getSpo2() {
		return spo;
	}

	public void setSpo(Set<Products_Orders> spo) {
		this.spo = spo;
	}

	public Products( String pname, String introduce, double price) {
		super();
		this.pname = pname;
		this.introduce = introduce;
		this.price = price;
	}

	public Products() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "Products [pid=" + pid + ", pname=" + pname + ", introduce="
				+ introduce + ", price=" + price  + "]";
	}
	
	
}
//中间表实体类代码
@Entity
@Table(name="products_orders",catalog="work")
public class Products_Orders implements Serializable{
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
	
	@ManyToOne(targetEntity=Orders.class,fetch=FetchType.LAZY,cascade=CascadeType.ALL)
	@JoinColumn(name="oid")
	private Orders o;
	
	@ManyToOne(targetEntity=Products.class,fetch=FetchType.LAZY,cascade=CascadeType.ALL)
	@JoinColumn(name="pid")
	private Products p;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Orders getO() {
		return o;
	}
	public void setO(Orders o) {
		this.o = o;
	}
	public Products getP() {
		return p;
	}
	public void setP(Products p) {
		this.p = p;
	}
	
	public Products_Orders(Orders o, Products p) {
		super();
		this.o = o;
		this.p = p;
	}
	
	public Products_Orders() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Products_Orders [id=" + id + ", o=" + o + ", p=" + p + "]";
	}
	
	
	
}

//测试代码
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory.getSession();
		//查询订单
		Orders o = (Orders) session.get(Orders.class, 1);
		System.out.println(o.toString());
	}

}

运行报错:

Exception in thread "main" java.lang.StackOverflowError
	at java.lang.Integer.toString(Integer.java:303)
	at java.lang.Integer.toString(Integer.java:116)
	at java.lang.String.valueOf(String.java:2932)
	at java.lang.Integer.toString(Integer.java:682)
	at java.lang.String.valueOf(String.java:2827)
	at java.lang.StringBuilder.append(StringBuilder.java:115)
	at com.szy.entity.Orders.toString(Orders.java:64)
	at java.lang.String.valueOf(String.java:2827)
	at java.lang.StringBuilder.append(StringBuilder.java:115)
	at com.szy.entity.Products_Orders.toString(Products_Orders.java:60)
	at java.lang.String.valueOf(String.java:2827)
	at java.lang.StringBuilder.append(StringBuilder.java:115)
	at java.util.AbstractCollection.toString(AbstractCollection.java:422)

原因分析:

问题的分析:
在运行时调用了Orders实体类的toString()方法,在方法中对中间类又进行了查询,造成了一个死循环。也就是在spo那个地方

@Override
	public String toString() {
		return "Orders [oid=" + oid + ", type=" + type +",spo"+spo+ "]";
	}

解决方案:

具体解决方案:
重写toString()方法,不再对中间类进行查询,打破死循环!就是把spo去掉之后再调用toString()方法就不会出错了。

@Override
	public String toString() {
		return "Orders [oid=" + oid + ", type=" + type + "]";
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值