6 Replies Latest reply on Jun 24, 2011 10:26 AM by Felix Roberto Read Rivero

    Help With Inheritance

    Felix Roberto Read Rivero Newbie

      I am triying to define a table structure

      with the following logic.

       

      If i have a DataType By example Person,

      a person can be one of 3 types of persons

      he can be a costumer or a material supplier.

       

      the problem is i want to have only on register of person,

      but a person can be either a costumer and/or a supplier,

      meaning he can be both.

       

      How can i represent this in a object structure without

      the use of descriminator?

      so that in the end i can have something like the code bellow.

       

       

      @Entity(name="TPerson")

      @Inheritance(strategy=InheritanceType.JOINED)

      abstract class Person {

          String id;

          String name;

      }

       

      @Entity(name="TCostumer")

      class Costumer extends Person {

          Date birthdate;

      }

       

      @Entity(name="TSupplier")

      class Supplier extends Person {

          String companyName;

      }

        • 2. Re: Help With Inheritance
          Nick Belaevski Master

          Hi Felix,

           

          This forum is dedicated to RichFaces, so I'm going to move this thread to Persistence forum.

          • 3. Re: Help With Inheritance
            Wolf-Dieter Fink Master

            I'm not sure about your problem

             

            If you implement like this and create two persons (one as supplier and one as customer) they will be different, but if I understand right you want to achieve that a single person is a customer AND a supplier right?

            • 4. Re: Help With Inheritance
              Felix Roberto Read Rivero Newbie

              yes

               

               

              TPerson

              id name

              1  xxxxxxxx

               

               

              TCostumer

              id  birthdate

              1   xx/xx/xxxx

               

               

              TSupplier

              id  companyName

              1   xxxxxxxxxxxxx

              • 5. Re: Help With Inheritance
                Wolf-Dieter Fink Master

                Mmm,

                maybe two tables related to TPerson with a 0..1-1 relation might be a solution.

                And TPerson contain a method isCustomer() { return custAddon != null } => custAddon.getBirthday()

                And the same for supplier.

                • 6. Re: Help With Inheritance
                  Felix Roberto Read Rivero Newbie

                  It seems the problem is in the mapping, cause acording to recomendations, each record of TPerson should have a relations ship with only one of the 2 tables(TSupplier or TCostumer) exclusively, and this because when the SQL Query is generated is contains and explicit INNER JOIN.

                   

                  A Examplo of the problem :

                   

                  Lets Asume a ORACLE 11g database,

                   

                  there is a Record of TCostumer with id 1, and there still no record in TSupplier.

                   

                  we would have

                   

                  TPerson

                  id name

                  1  xxxxxxxx

                   

                  TCostumer

                  id  birthdate

                  1   xx/xx/xxxx

                   

                  TSupplier

                  id  companyName

                   

                  so you have a registerd client, and this client is not yet a supplier.

                  But it so happen that one day this client opens a bussiness and

                  has to be register as a supplier with out lossing his rol as client.

                  So now that we understand the problem we proceed:

                   

                  1.- We check if there exists a supplier with id 1, at this moment

                  the Generated SQL would be a

                  "Select p.id, p.name, s.companyName from TPerson p,TSupplier s where p.id=s.id",

                   

                  in this case the result would be that it does not exists, cause its a inner join and there

                  does not exists a record in both tables.

                   

                  2.- even so we can verified that there exits a person, in this case the SQL would

                  be something like

                  "Select p.id, decode(p.id,s.id,"Supplier.class",c.id,"Costumer.class")  from TPerson p, TSupplier s, TCostumer c where p.id=s.id(+) and p.id=c.id(+)"

                  and the result would be that the record is found in TCostumer,

                  but the requested entitie was TSupplier wich would result in a type compatibility error, and that would happen

                  cause we where triying to look for a abstract Person and than a LEFT JOIN is applied for both tables (TCostumer, TSupplier)

                   

                  3.-But even so, we insist and we try to create a new Supplier, we call new Supplier(), we do the required sets,

                  and the we execute persist. the result would be a duplicated PK Error, that because we where triying to insert into

                  both tables, (TPerson and TSupplier), considering that acording to step 1 that entity does not exists.

                   

                  Hopes this explains the problem a little more clearly.