0 Replies Latest reply on Mar 22, 2005 8:46 PM by Yevgeny Shakhnovich

    One or zero to many relationship

    Yevgeny Shakhnovich Newbie

      I am not sure that my question is specific to JBoss. It may be more related to EJB in general.
      I wonder if it is possible to define a relationship like ONE or ZERO to MANY.

      For example, I have two beans CheckingAccount and SavingAccount. They are two different beans with different DB tables but they have local interfaces derived from the same AccountLocal interface.

      I also have a bean Transaction. Each instance of it is either for Checking or for Saving account, but not for both. This is why the relationship is not ONE to MANY but ONE or ZERO to MANY.

      I want to create two unidirectional realtionships between each of accounts and Transaction bean. In transaction bean, I want to use the same cmr field for both relationships. It seems simple since I can use the base AccountLocal interface as a type of the cmr field in Transaction. Both accounts, CheckingAccount and SavingAccount have cmr field for Collection of transactions. Access is unidirectional from accounts to transactions. Everything looks OK.

      I tested it with JBoss 3.2.7 and it worked as expected until I decided to delete an instance of Transaction bean. Completely according to the spec, EJB container tried to delete the instance from both relationships, of course failed in one case to find the other side of the relationship (each Transaction is really only in one collection) and threw FinderException.

      Eventually I succeeded by setting null to cmr-field in ejbRemove for Transaction. I assume, it told ejb container not to look for collections, but I don't know if it is the valid way to deal with such problems.
      What is the right solution for it?