3 Replies Latest reply on Mar 15, 2006 5:08 PM by Nikolaus Gerstmayr

    Cannot recognize polymorphic properties in a query

    Atanas Todorov Newbie

      I have a structure like this:

      @Entity
      public class User {
       Person person;
       //...
      }
      
      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public abstract class Person {...}
      
      @Entity
      public class Student extends Person {
       SomeType someStudentProperty;
       //...
      }
      
      @Entity
      public class Teacher extends Person {...}


      I want to query the users with testing the someStudentProperty in the where clause, but it doesn't work...
      "from User u where u.person.someStudentProperty = whatsoever..."

      ("from User u where u.person.somePersonProperty = whatsoever..." works correctly)

      Is it a bug, I miss something, or it is not a supported feature?

        • 1. Re: Cannot recognize polymorphic properties in a query
          Nikolaus Gerstmayr Newbie


          From my understanding of polymophism I wouldnt call this a polymorphic query:
          from User u where u.person.someStudentProperty = whatsoever.
          you are referring to a specific subclass in a "non polymorphic" way.
          (e.g violatinmg LSP)
          just imagine you would have write this query in plain java.

          I guess the semantics of the query should be:
          get all Students with certain studentproperties ...
          -->
          from Students s where s.person.someStudentProperty = should work.


          from User u where u.person.somePersonProperty = whatsoever..." works correctly ..

          yes, because this IS a polymorphic query whilst from User u where u.person.someStudentProperty = whatsoever aint ...

          -> IMHO: no bug, it's a feature :)

          hth, ntg




          • 2. Re: Cannot recognize polymorphic properties in a query
            Atanas Todorov Newbie

             

            From my understanding of polymophism I wouldnt call this a polymorphic query


            I did not say "polymorphic query". I used the term "polymorphic properties in a query". Anyway, it might be not the most proper way to call it. The question is about using properties declared in an inherited class of the ref type. Obviously the loaded "person" object will be of type Student or Teacher. What I'm interested in is how to query only the users having for "person" a student, that satisfies a certain condition...

            • 3. Re: Cannot recognize polymorphic properties in a query
              Nikolaus Gerstmayr Newbie

              [Quote]
              Anyway, it might be not the most proper way to call it.
              [/Quote]

              I'd call it subclass specific property ...
              (I dont like to call it polymorphic-property because they are very anti-polymorphic (polymophic in terms of generic programing) )

              Anyway, no matter how we call this -> about the question about using properties declared in an inherited class of the ref type.

              If you want to use a property only a certain subclass defines, you have to use that subclass. (obviously the superclass cant be used)

              superclass.subclasspecificproperty cant work.
              even in plain java.

              in other words: no, this kind of queries dont work.

              of course you can do a query for all users and find out
              via reflection which u.perons are studnents and for those
              which studentproperties= ?

              sorry for the long verison of it doesnt work ;)