how to insert a records in two tables from one entity (CMP)


EJB design: how to insert a records in two tables from one entity (CMP)

  1. hai everybody,

    we want to insert records in two tables using one
    entity(CMP) . in home interface we write two create methods.
    then there can be two findByPrimaryKey methods?
    how to write the .xml files?

    please suggest us a solution to do this in cmp , if this is not possible in CMP through how to do it in BMP.

    with regards
  2. Hi Murali,

    as per EJB specs one CMP bean can map to one Database table only. For inserting records into more than one table you have to use either BMP or semi-CMP.

    1. If you are going for BMP then write the insert queries for both the tables in ejbCreate(....) method.

    2. Another method is you can use CMP for inserting records in one table using ejbCreate() and write the insert queries for second records in ejbPostCreate(....) method.

    as ejbPostCreate(....) is called immediately after ejbCreate(....) you will be able to insert the record in second table. But in this case you have to write complet code for accessing database in ejbPostCreate(....). It is semi-CMP method. Here you are reducing your half burdon of writing queries for first table.