gpt4 book ai didi

java - Hibernate DataIntegrityViolationException 外键违规不允许删除

转载 作者:行者123 更新时间:2023-12-02 08:06:34 28 4
gpt4 key购买 nike

我有这样的关系。有一个 Car 表和 CarGroup 表。汽车组表中存放着汽车。我想要的是:当一辆车被移除时,如果它在一个汽车组内,它也应该像往常一样从那里移除。我在我的项目中使用 Spring 和 Hibernate(我下面有一个 PostgreSQL 数据库)。当我想删除一辆车时,我收到以下错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not delete: [com.aaa.bbb.ccc.Car#2]; SQL [delete from Car where a_id=? and st=?]; constraint [fkbc80f56cb1ece7b8]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [com.aaa.bbb.ccc.Car#2]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.java:582)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

我什至无法像往常一样从 pgadmin(postgresql 控制台)删除汽车。当我这样写时:

delete from Car where a_id=2 and st=2;

我得到的错误如下:

ERROR:  update or delete on table "car" violates foreign key constraint "fkac80f56cb1ece7b8" on table "cargroup_car"
DETAIL: Key (a_id)=(2) is still referenced from table "cargroup_car".

********** Error **********

ERROR: update or delete on table "car" violates foreign key constraint "fkac80f56cb1ece7b8" on table "cargroup_car"
SQL state: 23503
Detail: Key (a_id)=(2) is still referenced from table "cargroup_car".

我的 CarGroup 类的代码部分:

 @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinTable(name = "cargroup_car", joinColumns = @JoinColumn(name = "ADID"), inverseJoinColumns = @JoinColumn(name = "AID"))
private Set<Car> cars;

编辑:我希望:当我删除一辆车时,我希望它被删除,即使它位于汽车组下。我不想删除汽车组来删除汽车。我如何更改Java端数据库设置(级联类型或将多对多更改为多对一和一对多等)来做到这一点?

PS:我没有编写Java端代码。如果您解释一下,欢迎您。

最佳答案

您无法删除汽车,因为 cargroup_car 表中的一行引用了它。因此,您需要在删除汽车之前删除 cargroup_car 行。

关于java - Hibernate DataIntegrityViolationException 外键违规不允许删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071626/

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