Uploaded image for project: 'OpenJPA'
  1. OpenJPA
  2. OPENJPA-2651

IDs of entities are incorrectly assigned when @SqlResultSetMapping is used with inheritance and a ManyToOne relationship.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.2.2, 2.3.0, 2.4.1
    • Fix Version/s: 2.2.3, 2.4.0, 2.4.2
    • Component/s: sql
    • Labels:
      None

      Description

      I have discovered an issue with using @SqlResultSetMapping when inheritance and ManyToOne relationships are used. To explain the issue, take this entity and @SqlResultSetMapping:

      @Entity
      @SqlResultSetMapping(name = "MyResMap", entities = { @EntityResult(entityClass = CrtRequisicaoChequePersEntity.class, fields =

      { @FieldResult(name = "crtOperacaoByOperacaoRecepcaoServCent.id", column = "opRecepcaoServCentraisId"), @FieldResult(name = "crtOperacaoByOperacaoRecepcaoServCent.dataHora", column = "opRecepcaoServCentraisDataHora") }

      )
      })
      public class CrtRequisicaoChequePersEntity extends CrtRequisicaoEntity {
      .......
      @ManyToOne
      @javax.persistence.JoinColumn(name = "OPERACAO_RECEPCAO_SERV_CENT", referencedColumnName = "ID")
      private CrtOperacaoEntity crtOperacaoByOperacaoRecepcaoServCent;

      As you can see, this entity extends 'CrtRequisicaoEntity' and has a ManyToOne relationship to 'CrtOperacaoEntity', with name 'crtOperacaoByOperacaoRecepcaoServCent'. As you can see, the @FieldResult in the @SqlResultSetMapping references the fields in 'CrtOperacaoEntity'. These two entities are defined as follows:

      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public class CrtRequisicaoEntity {
      .....
      @Id
      private long id;
      .....

      @Entity
      public class CrtOperacaoEntity implements Serializable {
      .....
      @Id
      private long id;

      @Basic
      private Timestamp dataHora;

      With these entities, take an SQL select which uses an AS (the entire SQL is to long to add here, see provided recreate/test attached):

      String sqlCust = "SELECT t0.ID" +
      .........
      ",t2.DATA_HORA as opRecepcaoServCentraisDataHora" +
      ",t2.ID as opRecepcaoServCentraisId" +
      ....
      "FROM CrtRequisicaoChequePersEntity t0"
      ....
      "INNER JOIN CrtOperacaoEntity t2"
      ....

      With this SQL, the two IDs will be populated with the ID from CrtRequisicaoChequePersEntity, rather than the ID corresponding to each entity.

      Thanks,

      Heath

        Attachments

        1. OPENJPA-2651-2.2.x.patch
          15 kB
          Heath Thomann
        2. SqlResultSetMappingIssue.zip
          17 kB
          Heath Thomann

          Activity

            People

            • Assignee:
              jpaheath Heath Thomann
              Reporter:
              jpaheath Heath Thomann
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: