OpenJPA
  1. OpenJPA
  2. OPENJPA-1200

Incorrect Entity is returned if ElementClassCriteria is not selected.

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.0-M2
    • Fix Version/s: 1.1.1, 2.0.0-M3
    • Component/s: None
    • Labels:
      None
    • Patch Info:
      Patch Available
    1. openJPA-1200_1.1.x.patch
      19 kB
      Ravi P Palacherla
    2. OPENJPA-1200_trunk.patch
      17 kB
      Ravi P Palacherla

      Activity

      Hide
      Ravi P Palacherla added a comment -

      Attached patch contains testcase and a potential fix.

      Show
      Ravi P Palacherla added a comment - Attached patch contains testcase and a potential fix.
      Hide
      Ravi P Palacherla added a comment -

      Ref: http://n2.nabble.com/ClasscastException-when-ClassCrietria-is-not-used.-td3290568.html#a3290568

      I have an openJPA application with following entities:

      "Item" Entity with SINGLE_TABLE inheritance strategy.
      DiscriminatorColumn on this ITEM_TYPE.

      "Book" and "Movie" entities extend "Item."

      Book has Many to One relation with Artist.
      Movie has Many to One relation with Artist.

      "artist.getBooks()" results in rows that contain both "Book" and"Movie" hence I get the following classcast :
      Exception in thread "main" java.lang.ClassCastException: org.apache.openjpa.enhance.model$Movie$pcsubclass

      I think artist.getBooks() returning Movie is a BUG and hence opened this JIRA.

      Show
      Ravi P Palacherla added a comment - Ref: http://n2.nabble.com/ClasscastException-when-ClassCrietria-is-not-used.-td3290568.html#a3290568 I have an openJPA application with following entities: "Item" Entity with SINGLE_TABLE inheritance strategy. DiscriminatorColumn on this ITEM_TYPE. "Book" and "Movie" entities extend "Item." Book has Many to One relation with Artist. Movie has Many to One relation with Artist. "artist.getBooks()" results in rows that contain both "Book" and"Movie" hence I get the following classcast : Exception in thread "main" java.lang.ClassCastException: org.apache.openjpa.enhance.model$Movie$pcsubclass I think artist.getBooks() returning Movie is a BUG and hence opened this JIRA.
      Hide
      Ravi P Palacherla added a comment -

      With attached patch, TestFinderCache is failing.

      I will look into it and will try to fix it.

      Show
      Ravi P Palacherla added a comment - With attached patch, TestFinderCache is failing. I will look into it and will try to fix it.
      Hide
      Ravi P Palacherla added a comment -

      Test case only.

      Show
      Ravi P Palacherla added a comment - Test case only.
      Hide
      Ravi P Palacherla added a comment -

      Testcase with Fix.

      Show
      Ravi P Palacherla added a comment - Testcase with Fix.
      Hide
      Ravi P Palacherla added a comment -

      Attached patch makes the fixed behavior as default ( as it is a bug).
      If users need to have the old behavior then they can use
      compatibility property superclassDiscriminatorStrategyByDefault=false.

      Show
      Ravi P Palacherla added a comment - Attached patch makes the fixed behavior as default ( as it is a bug). If users need to have the old behavior then they can use compatibility property superclassDiscriminatorStrategyByDefault=false.
      Hide
      Michael Dick added a comment -

      Thanks for the patch Ravi.

      Show
      Michael Dick added a comment - Thanks for the patch Ravi.
      Hide
      David Ezzio added a comment -

      Applied Ravi's patch to 1.1.x branch at rev 889871.

      Show
      David Ezzio added a comment - Applied Ravi's patch to 1.1.x branch at rev 889871.
      Hide
      Rick Curtis added a comment -

      I know this is wayyyyy after the fact, but could you document the compatibility flag (SuperclassDiscriminatorStrategyByDefault)?

      The problem that was reported by OPENJPA-1398 was fixed by this flag, but it was pretty hard to find since it isn't documented.

      Show
      Rick Curtis added a comment - I know this is wayyyyy after the fact, but could you document the compatibility flag (SuperclassDiscriminatorStrategyByDefault)? The problem that was reported by OPENJPA-1398 was fixed by this flag, but it was pretty hard to find since it isn't documented.
      Hide
      Ravi P Palacherla added a comment -

      Hi Rick,

      I will document it.

      The reason I did not document it before is because the behavior seen with flag superclassDiscriminatorStrategyByDefault=false (pre-1200) is a BUG.
      So I thought I should not document something that is a BUG.

      Regards,
      Ravi.

      Show
      Ravi P Palacherla added a comment - Hi Rick, I will document it. The reason I did not document it before is because the behavior seen with flag superclassDiscriminatorStrategyByDefault=false (pre-1200) is a BUG. So I thought I should not document something that is a BUG. Regards, Ravi.
      Hide
      Rick Curtis added a comment -

      Thanks Ravi!

      Show
      Rick Curtis added a comment - Thanks Ravi!
      Hide
      Pinaki Poddar added a comment -

      Not sure of this option or even the original cause as 'error'. The domain model presented where Book and Movie both can have many Artists. And hence an Artist can be related to both Movies and Books. Thus Artist.getBooks() to return only books is the appropriate scenario for ElementClassCriteria (in fact if my memory serves me right, that is the exact reason to demonstrate the advanced capabilities of OpenJPA that domain model had been originally designed).

      The original use case will behave correctly if @ElementClassCriteria is used – it is that simple. Otherwise Artist.getItems() will return a list of Item containing both Book and Movies.

      I

      Show
      Pinaki Poddar added a comment - Not sure of this option or even the original cause as 'error'. The domain model presented where Book and Movie both can have many Artists. And hence an Artist can be related to both Movies and Books. Thus Artist.getBooks() to return only books is the appropriate scenario for ElementClassCriteria (in fact if my memory serves me right, that is the exact reason to demonstrate the advanced capabilities of OpenJPA that domain model had been originally designed). The original use case will behave correctly if @ElementClassCriteria is used – it is that simple. Otherwise Artist.getItems() will return a list of Item containing both Book and Movies. I
      Hide
      Ravi P Palacherla added a comment -

      Hi Pinaki,

      The issue is not about Artist.getItems().
      The issue is about when I do Artist.getBOOKS() then movies are also returned, which I guess is an error.

      Book extend Item and Movie extend Item.
      Artist has one to many relation with both Book and Movie.

      In this scenario, when I do artist.getBooks() then, I think, the expected behavior is to return only books and not movies.

      If you think it is not an error then I can switch the default value of superclassDiscriminatorStrategyByDefault to be false.
      This will return both books and movies when I do artist.getBooks().

      If a user do not want to get both books and movies when artist.getBooks() is called, then they can do one of the below :

      1. use openJPA specific annotation ElementClassCriteria to get only books.
      2. use superclassDiscriminatorStrategyByDefault = true to get only books.

      Please suggest.

      Thanks,
      Ravi.

      Show
      Ravi P Palacherla added a comment - Hi Pinaki, The issue is not about Artist.getItems(). The issue is about when I do Artist.getBOOKS() then movies are also returned, which I guess is an error. Book extend Item and Movie extend Item. Artist has one to many relation with both Book and Movie. In this scenario, when I do artist.getBooks() then, I think, the expected behavior is to return only books and not movies. If you think it is not an error then I can switch the default value of superclassDiscriminatorStrategyByDefault to be false. This will return both books and movies when I do artist.getBooks(). If a user do not want to get both books and movies when artist.getBooks() is called, then they can do one of the below : 1. use openJPA specific annotation ElementClassCriteria to get only books. 2. use superclassDiscriminatorStrategyByDefault = true to get only books. Please suggest. Thanks, Ravi.
      Hide
      Pinaki Poddar added a comment -

      Book b = new Book("book");
      Movie m = new Movie("movie");
      Artist a = new Artist("Herman Hess");
      b.setArtist(a);
      m.setArtist(a);

      As the above code form the test shows, an Artist instance a is related to a Movie m and a Book b.

      And both Book and Movie sharing the single table have the same column name "ARTIST" though the relationship field is named 'artist1' and 'artist' respectively.

      Under such mapping, Artist indeed now related to both Movies and Books via the same column 'ARTIST'

      I see that as an 'quirky' usage which can be sorted out by @ElementClassCriteria to provide a semantics such that Artist.getBooks() or Artist.getMovies() still filter out the collection-valued relation by the class of its elements.

      Show
      Pinaki Poddar added a comment - Book b = new Book("book"); Movie m = new Movie("movie"); Artist a = new Artist("Herman Hess"); b.setArtist(a); m.setArtist(a); As the above code form the test shows, an Artist instance a is related to a Movie m and a Book b. And both Book and Movie sharing the single table have the same column name "ARTIST" though the relationship field is named 'artist1' and 'artist' respectively. Under such mapping, Artist indeed now related to both Movies and Books via the same column 'ARTIST' I see that as an 'quirky' usage which can be sorted out by @ElementClassCriteria to provide a semantics such that Artist.getBooks() or Artist.getMovies() still filter out the collection-valued relation by the class of its elements.
      Hide
      Ravi P Palacherla added a comment -

      I agree that ElementClassCriteria is the easy solution for this issue, but what is the purpose of having DiscriminatorColumn ?
      Doesn't it mean Book entity is distinguished by discriminatory value BOOKS and movie by MOVIES, meaning when artist .getBooks() is called then the discriminatory value of books should only be considered ?

      One more reason for working on this issue is because customer claims the use case works on other JPA provides, TopLink, without the need for proprietary annotations.

      From your previous notes, you are suggesting to revert the changes in this JIRA and use ElementClassCriteria as a solution.
      Also from OPENJPA-1398, it does not look like my changes cover all the scenarios properly.

      So, I think the best thing to do here is to revert all the changes that went as part of this JIRA and ask customer to use ElementClassCriteria.
      Please confirm if my understanding is correct.

      Show
      Ravi P Palacherla added a comment - I agree that ElementClassCriteria is the easy solution for this issue, but what is the purpose of having DiscriminatorColumn ? Doesn't it mean Book entity is distinguished by discriminatory value BOOKS and movie by MOVIES, meaning when artist .getBooks() is called then the discriminatory value of books should only be considered ? One more reason for working on this issue is because customer claims the use case works on other JPA provides, TopLink, without the need for proprietary annotations. From your previous notes, you are suggesting to revert the changes in this JIRA and use ElementClassCriteria as a solution. Also from OPENJPA-1398 , it does not look like my changes cover all the scenarios properly. So, I think the best thing to do here is to revert all the changes that went as part of this JIRA and ask customer to use ElementClassCriteria. Please confirm if my understanding is correct.

        People

        • Assignee:
          Ravi P Palacherla
          Reporter:
          Ravi P Palacherla
        • Votes:
          0 Vote for this issue
          Watchers:
          1 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved:

            Development