1 Reply Latest reply on Aug 14, 2006 11:31 AM by Brett Birschbach

    Inconsistent behavior of Discriminator

    Brett Birschbach Newbie

      I have entity superclass MYSUPERCLASS
      -------------------------------------------------------------------

      @Entity
      @Table(name="TABLE_A")
      @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name="MYDESCRIMCOLUMN")
      public abstract class MYSUPERCLASS{}


      I have entity subclass MYSUBCLASS
      -------------------------------------------------------------------
      @Entity
      @DiscriminatorValue("SUBTYPE")
      public class MYSUBCLASS extends MYSUPERCLASS{
      }


      I have seperate entity class MYCONTAINER that joins contains an object of MYSUBCLASS
      -------------------------------------------------------------------
      @Entity
      @Table(name="TABLE_B")
      public class MYCONTAINER{
       private Collection<MYSUBCLASS> mySubCollect;
      
       @OneToMany(fetch=FetchType.EAGER)
       @JoinColumns({...})
       public Collection<MYSUBCLASS> getMySubCollect(){
       return mySubCollect;
       }
      }



      If I do a find on MYSUBCLASS, the sql statement (from the jboss log)performed includes in the where clause: MYSUBCLASS1_.MYDESCRIMCOLUMN = 'SUBTYPE'. Therefore, my collection is correctly populated with only objects that meet the discrimination criteria.


      However, if I do a find on MYCONTAINER, the sql statement performed does NOT include in the where clause: MYSUBCLASS1_.MYDESCRIMCOLUMN = 'SUBTYPE'.

      Here are the results: Since the descriminator is not part of the query, the hibernate query (extracted from the jboss log) returns 1 row for each row in TABLE_A that matches the join, regardless of whether it meets the criteria of the descriminator. However, what's even more odd, is that even though the resulting Collection 'mySubCollect' has 1 object for each row in TABLE_A, each of these objects is the same object, representing the first row retrieved. (If I change my 'mySubCollect' from a Collection to a Set, these multiple rows are compressed into a single row, so it appears that the Collection is getting 1 copy of the same object for each row in the table, regardless of the data in that row.)

      Wierd as heck. The join doesn't appear to be using the discriminator at all, and is returning the first row retrieved once for each row in the table that matches the join without the discriminator.

      We are using EJB3 on JBoss 4.0.4GA, using the Hibernate 3 persistence engine.


      If anyone can shine any light on what I am doing wrong, it would be greatly appreciated.

      Thank you!
      -Brett Birschbach

        • 1. Re: Inconsistent behavior of Discriminator
          Brett Birschbach Newbie

          Ok, I did figure the wierd part out as to why I was getting multiples of the same object instead of the 10 different database rows. My Id was declared incorrectly, and was identical among the ten rows. Apparently the Java Persistence sees that the objects all have the same Id and, since there is already one in memory, decides to point at that one instead of loading the row from the database. So that makes sense.

          What still doesn't make sense, however, is that a discriminator is not used during join queries. I am still getting all 10 rows from the database, regardless of whether they fit the discriminator. Can anyone explain that one?

          Thanks!
          -Brett Birschbach