0 Replies Latest reply on Feb 16, 2004 1:36 PM by vineeth_varghese

    Data in join table apart from the foreign keys using EJB2.0

    vineeth_varghese

      I have a scenario like this
      -----------------------------

      TableA (col1 PK)

      TableB (col2 PK)

      TableC (col1 , col2, col3)
      -----------------------------
      (Col1 + col2) composite PK
      Col1 is FK(TableA)
      Col2 is FK(TableB)
      Col3 is a normal column

      ------------------------------------

      Now I have designed the EJB like this

      EJB1 (Entity Object for TableA)
      EJB2 (Entity Object for TableB)

      There is a many - many relationship between EJB1 and EJB2 with TableC as join table.

      I'm following the SessionFacade pattern. In the Session Bean , I create the entity for EJB1 and create the Entity for EJB2. In the CMR setter of EJB1, i associate the EJB2,(though after adding it to a collection), which populates data to TableC.

      In this way, the data is inserted into TableA, TableB and TableC like

      Table A
      -------
      col1
      ----
      1

      TableB
      ------
      col2
      ----
      1

      TableC
      ------
      col1 col2 col3
      -------------------
      1 1 null

      My requirement here is to insert data to col3 in the same transaction ie the data should get populated in the same transaction that inserts data into col1 and col2 of TableC.If the validation for col3 fails, all inserts should be rolled back.

      I tried this appraoch. I created EJB3 which maps to TableC and performed the following steps.

      Steps
      -----
      1) In session bean, create EJB1 and get the reference to
      the EJB1object
      2) In session bean, create EJB2 and get the reference to
      the EJB2object
      3) create new vector with EJB2object as an element.
      4) On EJB1object , call setCMREJB2(vector);
      (The above series of steps inserts data in all the table except for col3 in TableC)
      5) Try to get the handle for the EJBObject for table3
      using findByPK(new PK(col1, col2))
      6) But this fails as the previous transaction is not
      commited and findByPK fails. So Im not able to call
      setter for col3 in EJB3.

      Any suggestions :-)