macrotea-cn 2010-11-07 07:13
浏览 275
已采纳

hibernate级联获得数据出错

1.需求
我想通过获得user,然后获得user的group的信息,但是我不知道为何出现了这样神奇的异常?

2.代码
[code="java"]
package com.macrotea.hb.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tb_group")
public class Group {
private int id;
private String name;
private Set users=new HashSet();
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getName() {
return name;
}
@OneToMany(
mappedBy = "group", cascade = { CascadeType.ALL }
)
public Set getUsers() {
return users;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUsers(Set users) {
this.users = users;
}
@Override
public String toString() {
return "Group [id=" + id + ", name=" + name + ", users=" + users + "]";
}

}

package com.macrotea.hb.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="tb_user")
public class User implements Serializable{
private int id;
private String name;
private Group group;
@ManyToOne(
cascade={CascadeType.ALL},fetch=FetchType.EAGER
)
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [group=" + group + ", id=" + id + ", name=" + name + "]";
}

}

     //main方法的测试
private static void test_lazy_get() {
    //准备
    // @ManyToOne(
    // cascade={CascadeType.ALL},fetch=FetchType.EAGER
    // )
    // @OneToMany(
    // mappedBy = "group", cascade = { CascadeType.ALL }
    // )
    Session session=HibernateSessionFactory.getSession();
    Transaction transaction=session.beginTransaction();
    transaction.begin();
    User user=(User)session.get(User.class, 1);
    System.out.println("user:"+user);
    System.out.println("user的group:"+user.getGroup().toString());
    transaction.commit();
    session.close();
}

[/code]

3.出错信息:
Hibernate:

select
user0_.id as id1_1_,
user0_.group_id as group3_1_1_,
user0_.name as name1_1_,
group1_.id as id0_0_,
group1_.name as name0_0_

from
tb_user user0_

left outer join
tb_group group1_

on user0_.group_id=group1_.id

where
user0_.id=?
Hibernate:

select
users0_.group_id as group3_1_,
users0_.id as id1_,
users0_.id as id1_0_,
users0_.group_id as group3_1_0_,
users0_.name as name1_0_

from
tb_user users0_

where
users0_.group_id=?
Exception in thread "main" java.lang.StackOverflowError
at java.lang.StringBuilder.append(StringBuilder.java:119)
at java.lang.StringBuilder.(StringBuilder.java:93)
at com.macrotea.hb.model.Group.toString(Group.java:44)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.User.toString(User.java:43)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at java.util.AbstractCollection.toString(AbstractCollection.java:422)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:333)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.Group.toString(Group.java:44)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.User.toString(User.java:43)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at java.util.AbstractCollection.toString(AbstractCollection.java:422)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:333)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.Group.toString(Group.java:44)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.User.toString(User.java:43)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at java.util.AbstractCollection.toString(AbstractCollection.java:422)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:333)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.Group.toString(Group.java:44)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.User.toString(User.java:43)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at java.util.AbstractCollection.toString(AbstractCollection.java:422)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:333)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.macrotea.hb.model.Group.toString(Group.java:44)
(略)

4.问题
不知道是不是自己的配置出了什么问题?

5.帖尾
小生在此感谢了。临帖涕零,不知所言!

  • 写回答

1条回答 默认 最新

  • yourname1111 2010-11-07 09:31
    关注

    toString方法不对,user打印了group,group里又打印user,无限循环,内存溢出

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化