3 Replies Latest reply on Nov 4, 2008 9:28 PM by Ben Coughenour

    DB inheritance mapping

    Ivan Pazmino Newbie

      Hi,


      I have a hierarchy similar to the following:



      abstract class Person {
          private String firstname;
          private String lastname;
          //getters & setters
      }
      
      class User extends Person {
          private String username;
          private String password;
          //getters & setters
      }
      
      class Visitor extends Person {
          private Date visitDate;
          //getters & setters
      }




      How can I map this into entites that can perform CRUD actions? And, how the actual tables in the DB would look like?


      Thanks in advance,


      IP

        • 1. Re: DB inheritance mapping
          Ingo Jobling Master

          Hi, try the Hibernate user forum. 

          • 2. Re: DB inheritance mapping
            Ivan Pazmino Newbie

            Hi,


            What I should do to make this hierarchy work as I want it to, I need to add some directives like



            <class name="Payment" table="PAYMENT">
                <id name="id" type="long" column="PAYMENT_ID">
                    <generator class="native"/>
                </id>
                <property name="amount" column="AMOUNT"/>
                ...
                <joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
                    <key column="PAYMENT_ID"/>
                    <property name="creditCardType" column="CCTYPE"/>
                    ...
                </joined-subclass>
            </class>




            in the mapping descriptor for hibernate.
            How can I modify the reveng.xml or persistence.xml file, so that I can have this configuration done when I generate the entities with the seam-gen tool?


            Thanks in advance


            IP

            • 3. Re: DB inheritance mapping
              Ben Coughenour Newbie

              Or you can use annotations.  I've set it up to put all three in one table below.



              @Entity
              @Table(name = "person")
              @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
              @DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.INTEGER)
              abstract class Person {
                  private String firstname;
                  private String lastname;
                  //getters & setters
              }
              
              @Entity
              @Table(name = "person")
              @DiscriminatorValue(value = "0")
              class User extends Person {
                  private String username;
                  private String password;
                  //getters & setters
              }
              
              @Entity
              @Table(name = "person")
              @DiscriminatorValue(value = "1")
              class Visitor extends Person {
                  private Date visitDate;
                  //getters & setters
              }