gpt4 book ai didi

java - 合并后无法删除分离的实体()

转载 作者:行者123 更新时间:2023-11-30 08:55:32 24 4
gpt4 key购买 nike

我在尝试删除实体时遇到问题,这是我的代码:

  @Transactional
private <K> void delete(String type, EntityWithId<K> entity) {
try {
em.remove(em.contains(entity) ? entity : em.merge(entity));
} catch (Exception e) {
logger.warn("Unable to delete " + type + " with id " + entity.getId());
}
}

我不太明白为什么,但是删除语句失败,抛出

java.lang.IllegalArgumentException: Removing a detached instance

实体管理器不包含实体,所以每次都会执行merge(),remove总是失败,传递的对象不一样。

我做错了什么吗?

这是捕获异常的 StackTrace:

Unable to delete log with id 14091612
java.lang.IllegalArgumentException: Removing a detached instance myproj.jpa.AdvertisementLog#14091612
at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:67)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:107)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:822)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:801)
at org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:880)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at com.sun.proxy.$Proxy59.remove(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at com.sun.proxy.$Proxy59.remove(Unknown Source)
at myproj.batch.impl.DeleteOldAdvertisementsImpl.delete(DeleteOldAdvertisementsImpl.java:93)
at myproj.batch.impl.DeleteOldAdvertisementsImpl.deleteAll(DeleteOldAdvertisementsImpl.java:86)
at myproj.batch.impl.DeleteOldAdvertisementsImpl.deleteAll(DeleteOldAdvertisementsImpl.java:80)
at myproj.batch.impl.DeleteOldAdvertisementsImpl.deleteAd(DeleteOldAdvertisementsImpl.java:69)
at myproj.batch.impl.DeleteOldAdvertisementsImpl.clean(DeleteOldAdvertisementsImpl.java:54)
at myproj.batch.main.DeleteOldAdvertisementsMain.main(DeleteOldAdvertisementsMain.java:13)

最佳答案

我看到注解在一个私有(private)方法上,所以它是从类中调用的。这个类是@Service类吗?您能否提供有关内部调用删除函数的方法的更多详细信息。私有(private)/ protected 方法忽略 Spring Transactional 注释。它们仅支持公共(public)方法。

编辑

仅将方法公开是不够的,bean 的入口点是什么(您从哪里调用该私有(private)方法)?如果该入口方法不是从 spring bean 调用的(例如,从 Autowiring 的字段)并且没有使用 @Transactional 注释,那么这就是问题的原因。

关于java - 合并后无法删除分离的实体(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947039/

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