Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-2522

JPAVariablePersister will not work when @Id is declared on a superclass

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

    XMLWordPrintable

Details

    • Low

    Description

      Given A and B:

      @MappedSuperclass
      public class A {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id = null;
      }

      @Entity
      public class B extends A {
      // Some other fields
      }

      In JPAVariablePersister:getClassIdValue, the code introspects the entity class to autodetect the @Id annotation. The code fails in this case since the annotation is declared on the superclass, and the current implementation only loops on the local fields of class B.

      I tested the following implementation of getClassIdValue to work (loop on parent class objects)

      private Long getClassIdValue(Object o)
      throws NoSuchMethodException, SecurityException,
      IllegalAccessException, InvocationTargetException,
      IllegalArgumentException {
      Class<?> c = o.getClass();
      Long idValue = null;
      do {
      Field fields[] = c.getDeclaredFields();
      for (int i = 0; i < fields.length; i++) {
      Id id = fields[i].getAnnotation(Id.class);
      if (id != null) {
      idValue = (Long) o.getClass().getMethod(
      "get"
      + Character.toUpperCase(fields[i]
      .getName().charAt(0))
      + fields[i].getName().substring(1),
      new Class<?>[] {}).invoke(o,
      new Object[] {});
      break;
      }
      }
      } while ((c = c.getSuperclass()) != null && idValue == null);

      return idValue;
      }

      Actually this type of code should probably deserve a service function somewhere...

      Attachments

        Activity

          People

            kverlaen@redhat.com Kris Verlaenen
            jserdaru_jira Julien Serdaru (Inactive)
            Archiver:
            rhn-support-ceverson Clark Everson

            Dates

              Created:
              Updated:
              Resolved:
              Archived:

              PagerDuty