首先,我已经对此进行了大量研究,但还没有找到这个例子,所以我希望答案不是重新设计你的数据库"。
使用Spring和JPA(以及hibernate)删除一个实体,并自动删除它的引用(或#34;孤儿"?)。
这是错误: DELETE语句与REFERENCE约束冲突" FK_ItemContainer_Item"。冲突发生在数据库" MyDatabase2",table" dbo.ItemContainer",column' itemId'。
以下是该方案:
@Entity
@Table(name = "Item")
public class Item implements java.io.Serializable {
private Integer itemId; //Auto Genenerated Unique ID
private String item; //Item description
private Set<ItemContainer> itemContainers = new HashSet<ItemContainer>(0); //Set of ItemContainers
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "item", orphanRemoval = true)
public Set<ItemContainer> getItemContainers() {
return this.itemContainers;
}
}
@Entity
@Table(name = "ItemContainer")
public class ItemContainer implements java.io.Serializable {
private Integer itemContainerId; //Auto Genenerated Unique ID
private Item item; //Item link
private Container container; //Container link
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "itemId")
public Item getItem() {
return this.item;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "containerId")
public Container getContainer() {
return this.container;
}
}
@Entity
@Table(name = "Container")
public class Container implements java.io.Serializable {
private Integer containerId; //Auto Genenerated Unique ID
private String container; //description
private Set<ItemContainer> itemContainers = new HashSet<ItemContainer>(0);
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "container", orphanRemoval = true)
public Set<ItemContainer> getItemContainers() {
return this.itemContainers;
}
}
容器和商品都可以存在独占性。 ItemContainer只能包含对现有项目和容器的引用
我想要做的是能够删除一个项目,并删除相应的ItemContainer行(使用孤立删除)。任何容器都应该存在。
以下是我尝试删除项目的方法:
@Override
public void delete(Item item) {
EntityManager em = getEntityManager();
em.remove(em.contains(item) ? item: em.merge(item));
}
我知道我可能必须首先手动删除ItemContainers,但是我一直在关注指南并帮助尝试让自动孤儿删除工作,并希望看到它。
感谢任何帮助。
干杯,
史蒂夫。
编辑1: 正如JB所要求的:这是使用的相关代码。
请求来自jsp到
/deleteItem?itemId=${item.itemId}
值得一提的是,我试图在我的jUnit测试中做同样的事情(如果需要,我可以发布测试代码和任何相关的类),但是我没有收到错误(它没有通过抛出FK约束错误,但ItemContainer表仍包含对已删除Item的引用。
项目控制器,服务和Dao实施:
@Controller
public class ItemController {
@Autowired
private ItemsService itemsService;
@RequestMapping(value = "/deleteItem")
public String deleteItem(@RequestParam() int itemId) {
itemsService.delete(itemId);
return "redirect:systemSettings/items";
}
}
@Service("itemsService")
public class ItemsService {
@Autowired
private ItemsDAO itemsDao;
public void delete(int itemId) {
itemsDao.delete(itemsDao.getItem(itemId));
}
}
@Repository
@Transactional
@Component("itemsDao")
public class ItemsDAOImpl implements ItemsDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
public EntityManager getEntityManager() {
return entityManager;
}
@Override
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Item getItem(int itemId) {
String queryString = "SELECT i FROM Item i "
+ "LEFT JOIN FETCH i.itemMachines "
+ "WHERE i.itemId=:itemId";
Query query = getEntityManager().createQuery(queryString);
query.setParameter("itemId", itemId);
Item item = (Item) query.getSingleResult();
System.out.println(item.getItemMachines().size());
return item;
}
@Override
public void delete(Item item) {
EntityManager em = getEntityManager();
em.remove(em.contains(item) ? item : em.merge(item));
}
}
这是堆栈跟踪:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:189)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:155)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:519)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy29.delete(Unknown Source)
at com.home.myproject.service.ItemsService.delete(ItemsService.java:45)
at com.home.myproject.service.ItemsService$$FastClassBySpringCGLIB$$788e7765.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:640)
at com.home.myproject.service.ItemsService$$EnhancerBySpringCGLIB$$296a7299.delete(<generated>)
at com.home.myproject.controllers.ItemController.deleteItem(ItemController.java:121)
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:483)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:515)
... 50 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_ItemContainer_Item". The conflict occurred in database "MyDatabase2", table "dbo.ItemContainer", column 'itemId'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 64 more
干杯。
史蒂夫。
编辑2:
发现这个问题JPA Cascade remove orphans - 詹姆斯&#39;答案证实了JB的意思,但仍然不知道为什么它不会工作:(
答案 0 :(得分:0)
第一个想法(原来是错的,请检查编辑解决方案)
好的,所以我已经深究这个问题。
首先让我先介绍一下解决方案(问题,因为我仍然有点困惑)。
我的项目中的POJO是通过将数据库逆向工程到java(使用eclipse中的hibernate工具插件)创建的。
这需要大量的试验和错误,但最终还是有效。
生成的POJO包含CascadeType.ALL
注释。
它之所以不起作用?
ItemContainer表的外键没有设置级联删除(鄙视在Java中生成注释)。
通过MS SQLServer Management Studio将两个外键设置为级联删除可以解决此问题。我现在可以删除一个Item并正确删除链接它们的ItemContainer记录。
编辑1:
-------------------------------------------------- ---------------------------- 强>
想想我会更新这个,因为新信息已经曝光。
cascade数据库选项acctualy cercumnavigates问题,而不是hibernate做级联,数据库正在这样做。
这不正确!!!
这个问题的罪魁祸首实际上是服务和DAO层之间的交互。我的DAO是一个交易类,我的服务不是这样(经过大量的研究,反之亦然)。
这是我的功能
public void delete(int itemId) {
itemsDao.delete(itemsDao.getItem(itemId));
}
<强>注意:强>
1.服务请求来自DAO的项目
2. DAO将项目返回服务
3.服务将项目传递给DAO
将@Transactional
添加到此方法(或将其从DAO中完全删除并将其设置为服务类)可以解决问题。
这背后的原因(根据我的理解)是@Transactional通知持久性引擎持久(或保持)其数据。一旦实体掉到@Transactional范围之外,它就会被刷新(或者可能被刷新),因此,传递回DAO的项目不再出现在实体管理器中(如果我的条款不准确,请原谅我)。 / p>
我希望这次是实际的解决方案;)