Questions about EJB 2.0 Relationship


EJB design: Questions about EJB 2.0 Relationship

  1. Questions about EJB 2.0 Relationship (1 messages)

    I am playing around with the EJB2.0 relationship support from BEA WLS6.0sp1. I just have some design questions.

    Let's take alook at a simple one to many relationship between a customer and n accounts he may have.

    In the database design, I would like to specify the foreign key column ctuomer_id in the table accounts as not null to ensure the referential integrity.

    In my code I would write

    Customer cust = customerHome.create(...);
    Account acc1 = accountHome.create(...);
    Account acc2 = accountHome.create(...);

    But I think everytime accountHome.create() is called, a new account record will be inserted into the database without the foreign key being specified. When the addAccount method is called, an update statement will set the the foreign key column. So I have to take out the not null clause when creating the table.

    I also encounter some design problem when implementing some advanced features. One thing I want to ensure that I don't put duplicates into a collection.

    public void addAccount(Accoutn acc){
    // actually some app logic dependent checking

    As shown in the de above, an account is created at first, which means a db record is inserted. After that addAccount is called where the duplicate checking is performed. If the account is in the collection already, the newly created entity must be removed again(with its record removed friom the DB.) I think it is a waste of time and resource to insert a record and delete it again.

    The other possibility is to postpone the account creation after the checking. But in that case the creation must be done in the addAccount method. It means a ejb-reference must be defined between the CustomerBean and the AccoutnBean.

    Any suggestions?


  2. Hi Dapeng,

    About database integrity, I think this seems to be the way the support is provided by the server.

    If I have understood the problem correctly, you are trying to create a new account for a customer, and trying to set it as value of the cmr field of the customer.

    I think you can re write the method this way.

    public void addAccount(Accoutn acc){
    // some app logic dependent checking ...

    Of course, I assume that you cannot create a account with the same attribute as one already existing in the database. Also, this is the only way to add accounts for a customer.

    On second thoughts about my assumptions, do you really care to check at all about the existence of the account with the customer ?

    I think if your application is sensitive to the presence of duplicates in the collection, you can specify java.util.Set as the *type* of the cmr-field.

    I hope this helps.

    Pramati Technologies,