How to handle EntityNotFoundException in Spring Data JPA (or just standard JPA 2)

10,188

Perhaps you are looking for the @NotFound annotation?

Here is a post that talks about it.

Share:
10,188
aym
Author by

aym

Updated on June 04, 2022

Comments

  • aym
    aym almost 2 years

    I am in the process of cleaning a database. These process involves changing the format of certain fields and getting rid of some data integrity issues.

    I developed a program with Spring Data 1.1 to process the records in batches. The problem arises with 2 entities in a @OneToOne relationship. The record for Entity B does not exist although Entity A has a reference to it. My job is to clear the reference to Entity B if that is the case.

    The question is: should I pre-process the data to clean this or can I adjust Spring Data or JPA settings to put null in the field if the Entity is not found?

    It is "normal" - with this data - to have a FK in Entity A that does not exist in Entity B, so I want to handle this in my code and not have to pre-process the data with an additional step or other tool. The data will be arriving in batches so any pre-processing makes things more complicated for the user.

    In summary, I want Spring Data to set the field to null and continue the process instead of getting an org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find....

  • JimHawkins
    JimHawkins almost 7 years
    link-only answers aren't useful for the community. What if the link gets broken?