gpt4 book ai didi

java - Hibernate Envers : an assertion failure occured (this may indicate a bug in Hibernate, 但更有可能是由于 session 的不安全使用)

转载 作者:搜寻专家 更新时间:2023-11-01 03:17:24 25 4
gpt4 key购买 nike

在我的 java 应用程序中,我使用 Hibernate Envers 来监听持续事件。我的监听器类看起来像这样。

@Component
public class DataCreationListener extends EnversPostInsertEventListenerImpl {

private static final long serialVersionUID = 1L;

@Autowired
DataService DataService;

public DataCreationListener() {
super(null);
}

@Override
public void onPostInsert(PostInsertEvent event) {

if (event.getEntity() instanceof DataDAO) {
Data Data = DataService.fromDao((DataDAO) event.getEntity());
// other stuff
}
}

}

fromDAO 方法中,我将 DAO(实体类型)对象转换为 api 类型对象。这是fromDAO方法

@Override
public Data fromDao(DataDAO data) {
if (data == null) {
return null;
}
Data api = new Data();
api.setId(data.getIdUser());
api.setAddress(data.getAddress());
api.setCity(data.getCity());
api.setCountry(getCountryFromId(data.getId()));

return api;
}

执行getCountryFromId(data.getId())时出现错误

该方法涉及错误发生的Spring Data JPA存储库调用。存储库调用如下所示

repository.findByCountry_Id(dataId);

在调试时我发现 dataId 有一个非空值,存储库是非空的,查询应该给出一个有效的结果。但是不知何故发生了这个错误

04:15:10.504 [http-nio-8080-exec-2] ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in my.app.dbAccess.models.DataDAO entry (don't flush the Session after an exception occurs)
org.hibernate.AssertionFailure: null id in my.app.dbAccess.models.DataDAO entry (don't flush the Session after an exception occurs)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:60)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:175)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:135)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85)
at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1251)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1319)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.count(SimpleJpaRepository.java:486)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy137.count(Unknown Source)
at my.app.dbAccess.generated.proxy.CountryRepositoryImpl.findByCountry_Id(CountryRepositoryImpl.java:800)
at my.app.services.DataServiceImpl.getCountryFromId(DataServiceImpl.java:131)
at my.app.services.DataServiceImpl.fromDao(DataServiceImpl.java:109)
at my.app.services.DataServiceImpl.fromDao(DataServiceImpl.java:1)
at my.app.services.DataServiceImpl.fromDao(DataServiceImpl.java:118)
at my.app.services.DataServiceImpl.fromDao(DataServiceImpl.java:1)
at my.app.services.DataServiceImpl$$FastClassBySpringCGLIB$$72be48ea.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:651)
at my.app.services.DataServiceImpl$$EnhancerBySpringCGLIB$$c0525c49.fromDao(<generated>)
at my.app.event.consumers.DataCreationListener.onPostInsert(DataCreationListener.java:40)
at org.hibernate.action.internal.EntityIdentityInsertAction.postInsert(EntityIdentityInsertAction.java:156)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:102)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)

是因为我正在从 onPostInsert 方法进行查询吗?通过这种方法进行查询不安全吗?我没有在数据中看到任何问题,我的直觉是它与 hibernate 环境有关,但我无法弄清楚。有人可以指出我正确的方法吗?谢谢!!

最佳答案

Is it because I am making a query from ithe onPostInsert method?

很有可能。据我所知,查询触发了预先刷新。
您更有可能使用 Hibernate 生成的 ID(很可能是数据库生成的)。

Is doing a query from this method not safe?

根据具体情况,它可能不安全。
可以肯定的是,它会破坏批量/批量插入,严重降低性能。

尝试暂停当前事务以防止刷新并在外部执行查询。
您可能会得到过时的结果,但在本例中这似乎不是问题。

不知道如何在 spring 中执行此操作,在 JavaEE 中,您可以调用 EJB 方法,例如:

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) // alternatively use REQUIRES_NEW
public Country getCountryFromId(Object id) {
....
}

我确定有一个 spring 等价物。

i don't see any problem in the data, my hunch is that it has something to do with hibernate envers but I can't figure out.

数据有问题:ID为空-尚未分配。
我使用应用程序生成的 ID(通常是 UUID 或 SUID)来避免此类问题。

Envers 也对监听器执行审计,但在 ST 中没有证据,我们也不知道哪个监听器先运行 - 你的还是 Envers。

在这两种情况下,这似乎都不相关。我会先检查其他内容(防止自动刷新)。

关于java - Hibernate Envers : an assertion failure occured (this may indicate a bug in Hibernate, 但更有可能是由于 session 的不安全使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599151/

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