gpt4 book ai didi

java - hibernate 保存时发生 StackOverflowError

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:51 26 4
gpt4 key购买 nike

我有一个 Student 表,其中有一个自动生成的 ID 作为主键,并且一对多映射到 Phone 表。

My Phone 表有一个复合主键 PhonePK,其中包含电话号码和 Student 表的外键 ID。

如果我只做 student.setPhones 而没有做 phonepk.setStudent,它会提示 id cannot be null。所以我正在设置 student.setPhones 和 phonePk.setStudent。但是现在我在 toString 上遇到了一个 stackoverflow 错误。

我真的不喜欢首先在两种方式上设置它,但不知道如何解决 id cannot be null 错误。我问过很多人,但他们帮不上忙。有人可以看看吗?

Student.java

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
@SuppressWarnings("serial")
public class Student implements Serializable {

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

private String fName;

private String lName;

private String mName;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id")
private Set<Phone> phones;

/**
* @return the fName
*/
public String getfName() {
return fName;
}

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @return the lName
*/
public String getlName() {
return lName;
}

/**
* @return the mName
*/
public String getmName() {
return mName;
}

/**
* @return the phones
*/
public Set<Phone> getPhones() {
return phones;
}

/**
* @param fName
* the fName to set
*/
public void setfName(final String fName) {
this.fName = fName;
}

/**
* @param id
* the id to set
*/
public void setId(final int id) {
this.id = id;
}

/**
* @param lName
* the lName to set
*/
public void setlName(final String lName) {
this.lName = lName;
}

/**
* @param mName
* the mName to set
*/
public void setmName(final String mName) {
this.mName = mName;
}

/**
* @param phones
* the phones to set
*/
public void setPhones(final Set<Phone> phones) {
this.phones = phones;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return String.format("Student [id=%s, fname=%s, lname=%s, mname=%s, phones=%s]",
id,
fName, lName, mName, phones);
}

}

Phone.java

import java.io.Serializable;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

@Entity
@SuppressWarnings("serial")
public class Phone implements Serializable {

@EmbeddedId
private PhonePK PK;

private String color;

/**
* @return the color
*/
public String getColor() {
return color;
}

public PhonePK getPK() {
return PK;
}

/**
* @param color
* the color to set
*/
public void setColor(final String color) {
this.color = color;
}

public void setPK(final PhonePK pK) {
PK = pK;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return String.format("Phone [PK=%s, color=%s]", PK, color);
}

}

PhonePK.java

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
@SuppressWarnings({ "serial" })
public class PhonePK implements Serializable {

@ManyToOne
@JoinColumn(name = "id", insertable = false, updatable = false)
private Student student;

private String phoneNumber;

public String getPhoneNumber() {
return phoneNumber;
}

public Student getStudent() {
return student;
}

public void setPhoneNumber(final String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public void setStudent(final Student student) {
this.student = student;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return String.format("PhonePK [student=%s, phoneNumber=%s]", student, phoneNumber);
}

}

主.java

import java.util.LinkedHashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {

public static void main(final String args[]) {

Configuration configuration = new Configuration();
Transaction transaction = null;

configuration.addAnnotatedClass(Student.class);
configuration.addAnnotatedClass(Phone.class);
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();

Student student = new Student();
student.setfName("Bob");
student.setlName("Buster");

Set<Phone> phones = new LinkedHashSet<Phone>();
Phone phone = new Phone();
phone.setColor("Black");
PhonePK phonePK = new PhonePK();
phonePK.setPhoneNumber("1111111111");
phonePK.setStudent(student); // Do not do this? But won't work (id cannot be null
error) if
// commented out??
phone.setPK(phonePK);
phones.add(phone);

student.setPhones(phones);

try {
transaction = session.beginTransaction();
System.out.println(student.toString()); // stackoverflow error!
session.save(student);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}

}
}

最佳答案

它的发生是因为您定义 toString() 方法的方式

Student 的 toString() 调用 Phone 的 toString() 调用 PhonePK 的 toString() 进而调用 Student 的 toString()...导致无限循环。


让我们详细看看它是如何发生的

在 Student toString() 中,因为其中有 phones 实例变量。它将遍历每个电话并调用 Phone toString()

public String toString() {
return String.format("Student [id=%s, fname=%s, lname=%s, mname=%s, phones=%s]",
id,
fName, lName, mName, phones);
}



在 Phone toString() 中,因为其中包含 PK 实例变量。它将调用 PhonePK toString()

public String toString() {
return String.format("Phone [PK=%s, color=%s]", PK, color);
}



在 PhonePK toString() 中,因为其中包含 phoneNumber 实例变量。它将调用 Phone toString()

public String toString() {
return String.format("PhonePK [student=%s, phoneNumber=%s]", student, phoneNumber);
}

关于java - hibernate 保存时发生 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17431543/

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