gpt4 book ai didi

hibernate - 在实体类中添加toString方法的JPA : java. lang.StackOverflowError

转载 作者:行者123 更新时间:2023-12-03 08:12:01 25 4
gpt4 key购买 nike

一切正常,直到我在实体类中添加toSting()为止。

之后,我开始在运行时收到以下错误:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at entity.Guide.toString(Guide.java:51)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at entity.Student.toString(Student.java:45)
...
@Entity
public class Teacher {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

private String name;

@OneToMany(mappedBy="teacher", cascade={CascadeType.PERSIST})
private Set<Student> students = new HashSet<Student>();

public Teacher() {}
public Teacher(String name) {
this.name = name;
}

public Set<Student> getStudents() {
return students;
}
public void addStudent(Student student) {
students.add(student);
student.setTeacher(this);
}
@Override
public String toString() {
return "Teacher[id=" + id + ", name=" + name
+ ", students=" + students + "]";
}

}
public class SnafuClient {
public static void main(String[] args) {

EntityManagerFactory emf = Persistence.createEntityManagerFactory("snafu");
EntityManager em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();

try {
txn.begin();

Query query = em.createQuery("select teacher from Teacher teacher");
List<Teacher> teachers = query.getResultList();

for (Teacher teacher: teachers) {
System.out.println(teacher);
}


txn.commit();
} catch(Exception e) {
if(txn != null) { txn.rollback(); }
e.printStackTrace();
} finally {
if(em != null) { em.close(); }
}

}
}

编辑:为学生实体添加的代码
@Entity
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

private String name;

@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name="teacher_id")
private Teacher teacher;

public Student() {}
public Student(String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
}

public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student [id=" + id +
+ ", name=" + name + ", teacher=" + teacher + "]";
}

}

最佳答案

根据学生类(class)的增加进行了更新
根据堆栈跟踪,您的问题与Student.toString()关联,因此发生了以下情况:
Teacher.toString()中,您通过将Student.toString()成员放置在students并置语句String中来隐式调用+ students +。在Student.toString()中,代码通过将teacher成员包括在String串联语句中来执行类似的操作。
这意味着调用Teacher.toString()Student.toString()将最终导致永无止境的循环,其中:Teacher.toString()隐式调用Student.toString(),后者又隐式调用Teacher.toString(),后者又调用Student.toString(),后者又调用...
这2个.toString()实现始终以无休止的循环来回调用,来回调用,来回调用,这最终使堆栈溢出并产生java.lang.StackOverflowError
若要更正此问题,应删除对实体的.toString()方法的隐式引用。作为替代,您可以让Teacher.toString()简单地输出length()集合的students,并可能包括Student名称的列表。在Student.toString()中,只需包含Teacher.name成员。

关于hibernate - 在实体类中添加toString方法的JPA : java. lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com