1 Reply Latest reply on Oct 24, 2001 5:24 AM by Fusayuki Minamoto

    EJB Design Question:

    Dave Elliot Newbie

      Hi all,

      This is a question that has perplexed myself & collegues:

      Say you have an entity bean (BMP) with some field as a primary key. The client wants a non-key attribute of that Bean to be enforced as unique. Example is a Country object...primary key is a String (eg "AU"), with a String 'name' attribute (eg "Australia") that we'd like to make unique.

      How do you enforce that the name attribute (eg "Australia") will be unique during calls to setName() ?

      * Composite primary key? This only enforces that the combination of fields is unique, not a particular single field.

      * Database contraints? Ugly in EJB, as the constraint violation isn't enforced as part of setName(), but later when ejbStore() occurs.

      * Searching the database for the existance of a given name whenever setName is called? Expensive and open to race conditions.

      * Making any unique-enforced bean attribute an Entity in it's own right? Lots of overhead...

      Any thoughts out there? Also seems nastier still in a CMP situation...


        • 1. Re: EJB Design Question:
          Fusayuki Minamoto Apprentice


          As a inhouse developer, I'd take the ugly database constrains option because it is so easy to implement/test and clients can retry the operation when they catch the ejb exception. I accept this if the transaction is relatively short.

          Otherwise, it is possible I might define an application exception for constratin violation and set 'requiresNew' transaction attribute to the entity. When a session bean catches the exception from the entity, the session bean can control the whole transaction with setRollbackOnly method.