我正在使用休眠进行 DBS 映射。我有文档类:
@Entity
@Table(name = "DOKU")
public class Dokument implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column
private String val;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "RELATIVES",
joinColumns = {@JoinColumn(name = "DOK1_ID")},
inverseJoinColumns = {@JoinColumn(name = "DOK2_ID")})
private Set<Dokument> iPoint = new HashSet<>();
@ManyToMany(mappedBy = "iPoint", cascade = CascadeType.ALL)
private Set<Dokument> pointMe = new HashSet<>();
和测试代码:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Dokument dok = new Dokument("master");
Dokument d1 = new Dokument("slave");
dok.getiPoint().add(d1);
session.save(dok);
session.save(d1);
session.getTransaction().commit();
session.close();
它将所有内容正确保存在 DBS 中。当我试图通过以下方式读回它时:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Dokument read = (Dokument) session.get(Dokument.class, 31);
System.out.println(read.getiPoint());
session.close();
我得到由 getiPoint 引起的堆栈溢出异常。session.get 自己工作。你看到代码中有任何错误吗?谢谢你。
//编辑我让Netbeans生成那些方法,还是同样的错误
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + this.id;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Dokument other = (Dokument) obj;
if (this.id != other.id) {
return false;
}
return true;
}