0

When I use JPA delete, I get an error. I check the log and find an update SQL, I don't know why I execute the deleting, but it prints update SQL

java 1.8, spring-data-jpa-2.1.8

the source code:

cardApplyRepository.delete(CardApply.builder().activityId(23443L).build());

the log:

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)
2
  • Seems like it is inserting a row to the t_card_apply table with null value for card_base_required_id . Commented Jul 15, 2019 at 5:30
  • 1
    try using ".deleteInBatch()" option. first, u need to fetch the entity from the table or delete by using the primary key Commented Jul 15, 2019 at 6:27

2 Answers 2

2

When you pass an Entity to the delete method it should be managed at this point.

For example by using the findById method and passing the result to delete.

You are passing a non-managed entity as you build it on the fly.

So either fetch one first or use the deleteById method instead.

Sign up to request clarification or add additional context in comments.

Comments

0

We might not inspect your issue in detail unless you provide the code of CardApply and CardApplyRepository if you are using a custom repository implementation.

What I can get is, you want to delete an entity by id, so, I suggest you that just use the deleteById method of Spring data's CrudRepository like below:

cardApplyRepository.deleteById(23443L);

Hope this helps. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.