在将Spring升级到v.6并将Sping Boot 升级到v.3(并自动将Hibernate升级到6.1.5)后,测试开始失败,原因是org.hibernate.AssertionFailure: force initializing collection loading
我有一个带有字段的实体类AClass
@OneToMany(mappedBy = "aClass", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<BClass> items;
当AClass的存储库触发多个方法时,hashCode方法被调用,当它尝试执行items.hashCode()
时,我得到异常:
2022-12-13T09:42:46.314+01:00 ERROR 15336 --- [ Test worker] org.hibernate.AssertionFailure : HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: force initializing collection loading
org.hibernate.AssertionFailure: force initializing collection loading
at app//org.hibernate.collection.spi.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:807)
at app//org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:995)
at app//org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:981)
at app//org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:170)
at app//org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:32)
at app//org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:443)
at app//org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:166)
at app//org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:91)
at app//org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31)
at app//org.hibernate.loader.ast.internal.CollectionLoaderSingleKey.load(CollectionLoaderSingleKey.java:121)
at app//org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:789)
at app//org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at app//org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
at app//org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1710)
at app//org.hibernate.collection.spi.AbstractPersistentCollection.lambda$initialize$3(AbstractPersistentCollection.java:613)
at app//org.hibernate.collection.spi.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:265)
at app//org.hibernate.collection.spi.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:611)
at app//org.hibernate.collection.spi.AbstractPersistentCollection.read(AbstractPersistentCollection.java:136)
at app//org.hibernate.collection.spi.PersistentSet.hashCode(PersistentSet.java:407)
at app//xyz.example.AClass.hashCode(AClass.java:52)
...
当我使用默认的Lombok hashCode方法,或者IntelliJ默认的
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (x != null ? x.hashCode() : 0);
result = 31 * result + (y != null ? y.hashCode() : 0);
result = 31 * result + (z != null ? z.hashCode() : 0);
result = 31 * result + (items != null ? items.hashCode() : 0);
return result;
}
我得到异常,但当我删除这行代码时:
result = 31 * result + (items != null ? items.hashCode() : 0);
测试通过但是,我不想从hashCode方法中删除items
1条答案
按热度按时间bvjveswy1#
过去有一个已知的问题,即休眠不是抛出
LazyInitializationException
,而是以某种方式消耗它并抛出AssertionFailure
。我会用
@Transactional
标记测试调用的方法,以确保实体会话在所有方法执行期间都是打开的,如果测试通过,这将表明Hibernate中存在bug。