5 Replies Latest reply on Jun 20, 2005 5:32 AM by epbernard

    Preview 5 -- Bug when Inheritance used with @OneToMany

      Take these three objects:

      Person, Pet, Cat, and Dog.

      Cat and Dog inherit from Pet using SINGLE_TABLE; id #1 is a Cat and id #2 is a Dog.

      Person contains a @OneToMany reference to Cat and a seperate @OneToMany reference to Dog. That means there's both a getCats() and getDogs() method in Person.

      If you use entityManager.find(Pet.class, 1), you will get a Cat object as expected.

      If you do person.getCats(), all of the objects in the returned collection (which is lazy loaded) will be of the correct type, Cat, but some of the items in the collection will in fact be Dogs (that is, you'll get Pet id #1 and id #2 in the collection, when you only should have gotten id #1, as it is the only Cat). Looking at the query that hibernate created, you'll see that hibernate didn't even attempt to select the disriminator field. This seems like a bug.

      However, if you remove the getCats and getDogs methods and replace them with a single getPets method which has a @OneToMany relationship with the base-type, Pet, then the objects in the collection are of the correct type with the correct data. The problem with this is that in some cases, we're only interested in Cats or Dogs, but not both.

      It seems that making a reference to a sub-class should work as expected.

      Let me know if you think it's a bug and I'll post to JIRA as appropriate.