当我使用JPA删除时,出现错误。我查看了日志并找到了更新SQL,但我不知道为什么执行删除操作,但是会显示更新SQL
java 1.8,spring-data-jpa-2.1.8
源代码:
cardApplyRepository.delete(CardApply.builder().activityId(23443L).build());
日志:
2019-07-15 13:09:20.478 [main] INFO o.s.t.w.s.TestDispatcherServlet [initServletBean:546] - Completed initialization in 19 ms
Hibernate:
insert
into
t_card_apply
(activity_id, activity_name, app_id, card_base_required_id, card_id, mass_msg_id, seller_id)
values
(?, ?, ?, ?, ?, ?, ?)
2019-07-15 13:09:20.573 [main] WARN o.h.e.j.s.SqlExceptionHelper [logExceptions:137] - SQL Error: 1048, SQLState: 23000
2019-07-15 13:09:20.574 [main] ERROR o.h.e.j.s.SqlExceptionHelper [logExceptions:142] - Column 'card_base_required_id' cannot be null
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:296)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy206.delete(Unknown Source)
at com.geek.icem.wechat.service.impl.WxCardServiceImplTest.deleteCard(WxCardServiceImplTest.java:34)
答案 0 :(得分:1)
当您将实体传递给delete
方法时,此时应该对其进行管理。
例如,通过使用findById
方法并将结果传递到delete
。
您正在动态传递一个非托管实体。
因此,要么先获取一个,要么使用deleteById
方法。
答案 1 :(得分:0)
如果您使用的是自定义存储库实现,除非您提供代码CardApply
和CardApplyRepository
,否则我们可能不会详细检查您的问题。
我能得到的是,您想通过id删除实体,因此,建议您像下面这样使用Spring数据deleteById
的{{1}}方法:
CrudRepository
希望这会有所帮助。祝你好运!
答案 2 :(得分:0)
从消息中我看到的是您正在尝试插入记录而不是更新/删除。
SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException:
2019-07-15 13:09:20.573 [main]警告o.h.e.j.s.SqlExceptionHelper [logExceptions:137]-SQL错误:1048,SQLState:23000 2019-07-15 13:09:20.574 [main]错误o.h.e.j.s.SqlExceptionHelper [logExceptions:142]-列'card_base_required_id'不能为 空
您可以通过使用deleteById(id)
使用Spring&JPA提供的功能来删除记录。
(findBy [根据实体/模型的别名])
更改
cardApplyRepository.delete(CardApply.builder().activityId(23443L).build());
收件人
cardApplyRepository.deleteById(23443L);