1 Reply Latest reply on Jul 11, 2007 10:58 AM by Richard Teviotdale

    Class Inheritance and the EntityHome Framework Class

    Richard Teviotdale Newbie

      I've been using a

      public class BaseHome extends EntityHome<Base>

      class to manage persistence for a collection of sub class entities that inherit from Base.

      For sub classes which have been persisted to the database things work as expected. I can setId(), getInstance(), and persist state changes to the database.

      I would like to create new entities using BaseHome, but I need a way to specify which concrete subclass should be instantiated. Here's what I've tried to do:
      public class BaseHome extends EntityHome<Base>
      {
       @Override
       public void create()
       {
       setEntityClass(Subclass.class);
       super.create();
       }
      }
      
      public class Subclass extends Base{}
      

      With the result:
      The method setEntityClass(Class<Base>)
      in the type Home<EntityManager,Base>
      is not applicable for the arguments (Class<Subclass>)


      Am I using the EntityHome framework in a manner that was never intended? Should I build my own bean for managing this object heirachy?

      As always, I greatly appreciate the insights the members of this forum share.

        • 1. Re: Class Inheritance and the EntityHome Framework Class
          Richard Teviotdale Newbie

          It turned out to be pretty simple to get it working.

          @Entity
          @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
          @DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
          public abstract class BaseClass implements Serializable
          {
           private String discriminator;
          
           @Column(name = "discriminator", length = 16, insertable = false, updatable = false)
           @Length(max = 16)
           public String getDiscriminator()
           {
           return discriminator;
           }
          
           public void setDiscriminator(String discriminator)
           {
           this.discriminator = discriminator;
           }
          }

          @Entity
          @DiscriminatorValue("SubClassOne")
          public class SubClassOne extends BaseClass
          {
           public SubClassOne()
           {
           discriminator = "SubClassOne";
           }
          }

          @Entity
          @DiscriminatorValue("SubClassTwo")
          public class SubClassTwo extends BaseClass
          {
           public SubClassTwo()
           {
           discriminator = "SubClassTwo";
           }
          }


          @Name("ClassHome")
          public class ClassHome extends EntityHome<BaseClass>
          {
           // set a default subclass to instantiate
           private String discriminator = "SubClassOne";
          
           @Override
           protected BaseClass createInstance()
           {
           BaseClass baseClass;
           if("SubClassOne".equals(discriminator))
           {
           baseClass = new SubClassOne();
           }
           else if("SubClassTwo".equals(discriminator))
           {
           baseClass = new SubClassTwo();
           }
           else // unknown class
           {
           // throw exception
           }
           return baseClass;
           }
          
           public String getDiscriminator()
           {
           return discriminator;
           }
          
           public void setDiscriminator(String discriminator)
           {
           this.discriminator = discriminator;
           }
          }


          When I want to create a new SubClass I set a JSF control to call setDiscriminator in the BaseClassHome object which it uses in turn to determine the type of subclass to instantiate.

          I'd appreciate any comments or advice if this seems like a misuse of the seam framework. Thanks.