Discussions

EJB design: Inheritance in Entity Beans

  1. Inheritance in Entity Beans (2 messages)

    I am new to EJB's and I don't understand why inheritance in entity beans is so bad.

    I just watched Ed Roman's "EJB Design Strategies and Performance Optimizations" presentation to Java One 2000 conference. One thing that jumped out at me was Ed Roman's discussion of inheritance in entity beans. His example on page 40 of the slides shows an entity bean inheriting from a regular java class in order to reduce the number of hits the web server makes on the application server. Is this for real? I thought inheritance of any kind was not allowed in entity beans. Anyway, here's his code:

    public class ProductInfo implements java. io. Serializable {
      public String SKU;
      public String description;
    }

    public class Product extends ProductInfo implements javax. ejb. EntityBean {

       // All my fields are inherited from the value object!

       public ProductInfo getProductInfo() {
          ProductInfo info = new ProductInfo();
          info. SKU = this. SKU;
          info. description = this. description;
          return info;
       }

       public void setProductInfo( ProductInfo info) {
         this. SKU = info. SKU;
         this. description = info. description;
       }
    }

    As you can see, the entity bean Product inherits the two attributes, SKU and description, from class ProductInfo.

    Hmm.....anyone got feedback on this?

    Threaded Messages (2)

  2. Inheritance in Entity Beans[ Go to top ]

    There is nothing wrong in using inheritance. The example shown are perfectly alrite. However, u should be aware of
    some restrictions that are applied.

    For example, a class that inherits from an BMP class cannot have an ejbCreate() method that returns a different primary key class, since the ejbCreate() method must return a primary key.

    Check out the EJB Container/Server that u are using for more info.

    alex
  3. Inheritance in Entity Beans[ Go to top ]

    This isn't inheritance between entity beans.. rather it's an entity bean that inherits part of its class definition from a Java class. To the application server, it doesn't really know the difference between a plain old entity bean and this special entity bean. There isn't any legal problem with this.

    Note that this example will not work with EJB 2.0 CMP because EJB 2.0 CMP does not allow the developer to programmatically define his own container-managed fields.

    -Ed