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.帖尾
小生在此感谢了。临帖涕零,不知所言!