如何从`@ OneToMany`关系中删除两个表中的记录?

时间:2015-03-13 17:43:47

标签: spring jpa spring-data-jpa

我正在使用JPA和Spring Data来执行CRUD操作。在下面找到详细信息:

主要表:

@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 {

}

删除记录的Spring数据代码:

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)

有人能让我知道为什么我会收到stackoverflow错误吗?

注意:如果我将抓取类型从EAGER更改为LAZY并设置orphanRemoval=true,则可以正常工作。如果这是解决此问题的唯一方法,请告诉我们?

1 个答案:

答案 0 :(得分:0)

findAll()方法正在加载与所有大学相关联的所有学生,这可能是您收到StackOverflow错误的原因。这就是为什么当你使用LAZY fetch-type时它会消失,因为所有学生最初都不会被加载。