Discussions

J2EE patterns: Creating Dependent Entity Beans using Auto-PrimaryKey-Gen

  1. Let’s take a common database schema structure.
    Table fields
    ---------------
    SYSTEMUSER ID(pk), PERSONID(fk), USERID(fk),.......
    PERSON ID(pk),NAME,ADDRESSID(fk),..............
    ADDRESS ID(pk),STREET,CITYID(fk)................

    pk = PRIMARY-KEY
    fk = FOREIGN-KEY.

    As you can see each table has got a unique id and for each table there is an Entity Bean.

    I have used Weblogic6.1 AUTO-KEY-GEN facitlity to generate id for each entity bean. But the problem is each Entity Bean is having a foreign-key, whech we get from a previous Entity Bean.

    I have used Session-Facade Pattern with little difference.
    In the session bean invoke Entity Bean methos for e.g
    the session bean pesudo-code may look like

    1. create Address()
    2. get addressId = address.getId();
    3. create Person(addressId ).
    4. get presonId = person.getId();
    5. create SystemUser(personId);

    Thus we are not bothered how primary key of each bean is created. It is left upto the server. But this does force you with the entity bean dependency. You have to create Address, then Person and then SystemUser.

    I have seen few other posts, which try to do the same this by using BMP. I have tried to use CMP all the while.

    Moreovr if you are having relationship between the beans. You can pass foreign-keys by passing the refrence of the related bean.

    This is a fine-grain apprach to entity bean. But you can have ValueObject for each entity bean, which can be wrapped with Statless Session bean.

    Thus, my design makes best of SESSION-FACADE AND VALUE OBJECTS.

    I would be keen to know about the drawbacks for this approach.

    Regards,

    Dheeraj


  2. Hello

    First of all let’s establish from the scratch if your project requires one existing-designed-filled-before database or you are lucky and let’s say you have control over how you design/create the database. So because you want to use JEE CMP don’t use too much facilities/functionality provided/dependent by the appserver vendor/provider as pk auto generation. Very simple is to build your own framework which takes care about this like one interface (Long) IOIDGenerator.getOIDForEntityObject() which will be implemented in a specific manner for Oracle(which supports auto sequences), mySQL( you create your sequence tables …) etc. If the “paranoia” goes far you can associate to this value the hostid, ipaddress,systime etc. but in 90% of situations this is not needed … Another advice : because of very poor support of EJB QL for real business needs and the same applies for CMR, and as a plus the CMR has too restrictive rules/connection with the database design and can make you loose too much the OO Design which should be more important don’t design your table from the scratch with foreign keys/ restrictions you don’t need this, manage via the object model and workflow your objects interaction, maybe this can be a little more difficult( need to handle callback methods ejbXXX() ;-) and correct transactions definitions ) but we should be at first oo programmers and not deployment descriptors writers, and that's why personally I hate when I need to write/correct/modify complicated deployment descriptors because all deployment tools and appservers have bugs exactly here ...

    Regards,
  3. Lot of people have got less to say about CMP Entity beans. But they do offer lot facilities. If we over look them we have write our own Data Access classes and we wrap them with a Session Bean so that we can use the transactional capabilities of the Application server. This way I think we are hardly using good servers like weblogic to great extent.

    Dheeraj