0
我正在使用JPA和Spring數據來執行CRUD操作。請看下面的詳細信息:如何用`@ OneToMany`關係從兩個表中刪除記錄?
主表:
@Entity
@Table(name = "university")
public class University implements Serializable {
@OneToMany(mappedBy = "pk.university",
cascade = CascadeType.ALL,
fetch = FetchType.EAGER)
private Set<Student> tagDetailReaders = new HashSet<Student>(0);
}
外鍵表:
@Entity
@Table(name = "student")
@AssociationOverrides({ @AssociationOverride(name = "pk.university", joinColumns = @JoinColumn(name = "university_id")) })
public class Student implements Serializable {
}
春數據代碼刪除記錄:
University entity = universityDao.findAll().get(0);
universityDao.delete(entity);
我收到以下錯誤:
java.lang.StackOverflowError
at java.net.DualStackPlainSocketImpl.socketAvailable(Unknown Source)
at java.net.AbstractPlainSocketImpl.available(Unknown Source)
at java.net.SocketInputStream.available(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.available(ReadAheadInputStream.java:230)
at com.mysql.jdbc.MysqlIO.clearInputStream(MysqlIO.java:954)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2531)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:324)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2148)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:78)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:68)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
有人能告訴我爲什麼我收到計算器錯誤?
注:如果我改變提取類型從EAGER
到LAZY
並設置orphanRemoval=true
它工作正常。請讓我知道這是否是解決此問題的唯一方法?