gpt4 book ai didi

尝试删除实体时出现 java.lang.StackOverflowError

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

尝试删除实体时,会发生 StackOverflowError。唯一发生这种情况的是当我有一个包含 2 个外键引用的复合键时。我在下面创建了一个最小的示例:

第一个包含 @OneToMany 集合的对象:

@Entity
public class Foo {

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

@OneToMany(mappedBy = "id.foo", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Baz> baz;

// Getters and setters omitted.
}

包含 @OneToMany 引用的第二个对象:

@Entity
public class Bar {

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

@OneToMany(mappedBy = "id.bar", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Baz> baz;

// Getters and setters omitted.
}

包含上述两个对象(FooBar)作为复合键的对象:

@Entity
public class Baz {

@EmbeddedId
private BazId id;

// Getters and setters omitted.
}

最后是 Baz 的复合键类:

@Embeddable
public class BazId implements Serializable {

private static final long serialVersionUID = -8869241450494423817L;

@ManyToOne
private Foo foo;

@ManyToOne
private Bar bar;

// Getters and setters omitted
}

当我执行下面的测试时,我得到一个java.lang.StackOverflowError:

// Spring Data JPA Repositories (extend CrudRepository)
@Autowired private FooRepository fooRepository;
@Autowired private BarRepository barRepository;
@Autowired private BazRepository bazRepository;

@Test
public void test() {
final Foo foo = new Foo();
fooRepository.save(foo);

final Bar bar = new Bar();
barRepository.save(bar);

final Baz baz = new Baz(new BazId(foo, bar));
bazRepository.save(baz);

foo.getBaz().add(baz);
bar.getBaz().add(baz);

// These two deletes cause the StackOverflowError
fooRepository.delete(foo);
barRepository.delete(bar);
}

我尝试过的:

  • 重写equals(Object o)toString()hashCode()

除了完全重做我的映射之外,我不知道还能尝试什么。我需要以这种方式设置映射,就像在我的实际项目中一样,我有一个 User 跟随一个 Business,因此这需要是两个实体之间的组合键。为什么这会导致无限递归?

编辑

堆栈跟踪是否有帮助(部分。整个事情不适合):

java.lang.StackOverflowError
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:131)
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
at com.sun.proxy.$Proxy73.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1929)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1876)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:306)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2204)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:60)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:50)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3956)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1022)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:632)
at org.hibernate.type.EntityType.resolve(EntityType.java:424)
at org.hibernate.type.ComponentType.resolve(ComponentType.java:687)
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:848)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:306)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2204)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:60)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:50)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3956)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1022)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:632)
at org.hibernate.type.EntityType.resolve(EntityType.java:424)
at org.hibernate.type.ComponentType.resolve(ComponentType.java:687)
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:848)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)

最佳答案

FooBar 内的 Baz 集合上使用 cascade = CascadeType.ALL 会在尝试时导致无限递归删除。删除两个集合上的级联并手动删除 Baz 关系(而不是级联)来解决此问题。

关于尝试删除实体时出现 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403196/

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