2 Replies Latest reply on Mar 7, 2002 10:27 AM by Dain Sundstrom

    Setting CMR fields

    Noel Rocher Apprentice

      Hi all,


      I'm a bit confused.
      I have read a lot of relationship examples and it seems to have several ways to set the foreign key value.

      For example, in an "One-to-Many" relationship between an "Invoice" EJB and its "InvoiceLine" EJB.

      I've got the "Collection get/setLines()" accessor in the Invoice and the "InvoiceLocal get/setInvoice()" in the InvoiceLine.

      In my RDBMS I define a fk for InvoiceLine table named "invoice_id".

      In my InvoiceLine.ejbCreate(...) method, my arguments are all fiels except the "invoice_id" and then I use the setters for them.

      In a SessionBean (and in only one transaction), I:
      1)- create a new Invoice
      2)- create a new InvoiceLine
      3)- add the new line with the Invoice.getLines().add() method

      Is it wrong or this seems to be the rigth way ?


      Why this ejbPostCreate(){ setInvoice_id();} should be necessary ?

      Even worse the
      ejbPostCreate(){ lookup_for_Invoice_localhome + find_Invoice_by_primary_key + setInvoice(...);}

      example I read in the Java Connection article(http://developer.java.sun.com/developer/technicalArticles/ebeans/EJB20CMP/)

      :-(



      Is someone can tell me what is the best practices for doing this ?
      :o)


      Many thanks.

        • 1. Re: Setting CMR fields
          Noel Rocher Apprentice

          just to put this thread on top

          • 2. Re: Setting CMR fields
            Dain Sundstrom Master

            > In a SessionBean (and in only one transaction), I:
            > 1)- create a new Invoice
            > 2)- create a new InvoiceLine
            > 3)- add the new line with the
            > he Invoice.getLines().add() method
            >
            > Is it wrong or this seems to be the rigth way ?

            Yes

            > Why this ejbPostCreate(){ setInvoice_id();}
            > should be necessary ?

            Read the cmp chapter in the EJB 2.0 spec. A bean provider is not allowed to set a cmr field in ejbCreate.

            > Even worse the
            > ejbPostCreate(){ lookup_for_Invoice_localhome +
            > find_Invoice_by_primary_key + setInvoice(...);}


            Cache the JNDI lookup somewhere in your application. JNDI lookups are very expensive.

            Find by primary key is usually a single hashtable lookup, but if you want to make the code easier, pass the local invoice object.

            > example I read in the Java Connection
            > article(http://developer.java.sun.com/developer/techni
            > alArticles/ebeans/EJB20CMP/)

            I haven't

            > Is someone can tell me what is the best practices for
            > doing this ?

            Doing what?