Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1576

JpaMetamodelEntityInformation.getId(…) uses field access for proxied entities

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1.9 (Lovelace SR9)
    • Component/s: Core
    • Environment:

      Description

      When trying to delete an entity, nothing happens because spring is accessing the id directly, and because it is a hibernate proxy object, the member has default 0 value, thus it thinks it is a new entity and skips altogether the delete.

      From debugging, this is the relevant part of the callstack:

      getField:674, ReflectionUtils (org.springframework.util)
      getPropertyValue:63, DirectFieldAccessFallbackBeanWrapper (org.springframework.data.util)
      getId:154, JpaMetamodelEntityInformation (org.springframework.data.jpa.repository.support)
      isNew:42, AbstractEntityInformation (org.springframework.data.repository.core.support)
      isNew:233, JpaMetamodelEntityInformation (org.springframework.data.jpa.repository.support)
      delete:170, SimpleJpaRepository (org.springframework.data.jpa.repository.support)
      

      At the top, in `ReflectionUtils::getField(Field,Object)` method, the `target` variable is of type class `package.Class$HibernateProxy$HWnaDQWN`, and field is `private int package.Class.id`.

      Note that accessing the `id` by method `target.getId()` returns non-zero id, as expected.

      The `id` is defined inside the entity as follows:

          @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
          private int id;
      

      After more debugging the cause was the entity's id getter method was protected, changing it to public solved this problem.
      But shoudn't spring access using the protected method instead of direct member access?
      This issue was not present in spring 4, because I just updated to spring 5.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              schauder Jens Schauder
              Reporter:
              cdalxndr cda
              Last updater:
              Mark Paluch
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: