OpenJPA
  1. OpenJPA
  2. OPENJPA-660

ClassCastException when using OneToMany Relation and collection is subclass using Discriminator with SINGLE_TABLE strategy.

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.1.0, 1.1.1, 1.2.0
    • Fix Version/s: 1.2.0
    • Component/s: jpa
    • Labels:
      None

      Description

      If the entity has OneToMany relation and collection is declared as subclass which uses SINGLE_TABLE inheritance strategy, it fetches all rows irrespective of Discriminator value and throws ClassCastException.

      There is entity Department (table dept) having OneToMany relation with another entity FullTimeEmployee.

      @OneToMany (mappedBy="dept", cascade=CascadeType.ALL)
      private Collection<FullTimeEmployee> fullTimeEmployees;

      There is abstract class Employee with
      @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name="TYPE")

      There are two entity classes FullTimeEmployee and PartTimeEmployee which extends Employee (table emp) with Discriminator values 'F' and 'P' respectively.

      Now, suppose emp table contains 2 rows of PartTimeEmployee and 2 rows of FullTimeEmployee and if test class fetches Department object and calls dept.getFullTimeEmployees(), it throws ClassCastException as it gets 4 rows and doesn't use discriminator and subclass type while generating SQL query.

      1. testInheritance.zip
        22 kB
        Vikram Bhatia
      2. patch.txt
        4 kB
        Fay Wang

        Activity

        Vikram Bhatia created issue -
        Vikram Bhatia made changes -
        Field Original Value New Value
        Affects Version/s 1.0.2 [ 12312846 ]
        Affects Version/s 1.1.1 [ 12313177 ]
        Affects Version/s 1.0.3 [ 12312969 ]
        Affects Version/s 1.1.0 [ 12312344 ]
        Affects Version/s 1.0.4 [ 12313301 ]
        Affects Version/s 1.0.0 [ 12312341 ]
        Affects Version/s 1.2.0 [ 12313102 ]
        Affects Version/s 1.0.1 [ 12312687 ]
        Vikram Bhatia made changes -
        Attachment testInheritance.zip [ 12386166 ]
        Vikram Bhatia made changes -
        Description If the entity has OneToMany relation and collection is declared as subclass which uses SINGLE_TABLE inheritance strategy, it fetches all rows irrespective of Discriminator value and throws ClassCastException.

        There is entity Department having OneToMany relation with another entity FullTimeEmployee.

          @OneToMany (mappedBy="dept", cascade=CascadeType.ALL)
          private Collection<FullTimeEmployee> fullTimeEmployees;

        There is abstract class Employee with
        @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
        @DiscriminatorColumn(name="TYPE")

        There are two entity classes FullTimeEmployee and PartTimeEmployee which extends Employee (table emp) with Discriminator values 'F' and 'P' respectively.

        Now, Support emp table contains 2 rows of PartTimeEmployee and 2 rows of FullTimeEmployee and if test class fetches Department object and calls dept.getFullTimeEmployees(), it throws ClassCastException as it gets 4 rows and doesn't use discriminator and sub class type while generating SQL query.
        If the entity has OneToMany relation and collection is declared as subclass which uses SINGLE_TABLE inheritance strategy, it fetches all rows irrespective of Discriminator value and throws ClassCastException.

        There is entity Department (table dept) having OneToMany relation with another entity FullTimeEmployee.

          @OneToMany (mappedBy="dept", cascade=CascadeType.ALL)
          private Collection<FullTimeEmployee> fullTimeEmployees;

        There is abstract class Employee with
        @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
        @DiscriminatorColumn(name="TYPE")

        There are two entity classes FullTimeEmployee and PartTimeEmployee which extends Employee (table emp) with Discriminator values 'F' and 'P' respectively.

        Now, suppose emp table contains 2 rows of PartTimeEmployee and 2 rows of FullTimeEmployee and if test class fetches Department object and calls dept.getFullTimeEmployees(), it throws ClassCastException as it gets 4 rows and doesn't use discriminator and subclass type while generating SQL query.
        Fay Wang made changes -
        Attachment patch.txt [ 12386402 ]
        Pinaki Poddar made changes -
        Assignee Pinaki Poddar [ ppoddar@apache.org ]
        Pinaki Poddar made changes -
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Michael Dick made changes -
        Fix Version/s 1.2.0 [ 12313102 ]
        Donald Woods made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Pinaki Poddar
            Reporter:
            Vikram Bhatia
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development