finding several bean types with a common home interface


EJB design: finding several bean types with a common home interface

  1. I'd like to have a single Home interface that can find several different Bean Classes (all implementing a common interface, which is what the home finder would return), and I'd like to still have a Bean Class specific home interface. How can I do this -- the ejb-jar.xml specifies one required ejb-class per deployment descriptor.

  2. Hi Dan,

    One of the standard "tricks" to achieve this in EJB is to do the following. First, encode in your primary key the bean's type. For example, if your bean's type is a String, you can encode both the String PK and the bean's type in that single String. You could separate the two by a colon, for example.

    Next, call your finder on the parent Home interface. This gives you back all your beans. The problem is that they are all of the parent bean type, when in fact you want some of them to be of a child bean type. You then iterate through your list of beans, retrieve the primary key for each bean, decode from the PK the true bean type, then call findByPrimaryKey() on the true bean type's home interface.

    This is one of the standard "tricks" I've used with EJB 1.1. I'm not sure if EJB 2.0 addresses this issue. I suspect that it does not fully address it.

    Hope that helps. If something's not clear, just ask.

    David Sims
    Sims Computing
  3. David, thanks for responding --

    How can we have a Home interface that returns all parent beans and sub-classed beans, your response is somewhat confusing to me. Thanks. (If you could include example code of your Home interface, Bean class and remote interface, I would appreciate it.)

  4. Hi Dan,

    In your parent Home interface, you write a finder method that finds *all* relevant beans, including both parents and children. After all, it's just some SQL that gets executed to do the finder, more or less.

    At this point, you've got a bunch of beans that represent your parents and children.

    For example, if a child bean adds a couple columns to a database table, your parent finder method will ignore those. But your child beans will still be found. But then that's why you have to do a findByPrimaryKey() on the child Home interface -- in order to load those extra couple columns.

    Sorry that I can't provide a complete example -- kind of busy right now. Feel free to take this to email: david at