6 Replies Latest reply on Jul 14, 2003 3:56 AM by sreram

    CMP 2.0 not populating foreign key field for many to one

    F Zemen Newbie

      Scenario: Single table, parent-child relationship with "foreign key" parent field (a varchar) pointing to primary key (a varchar). MySQL. JBoss 3.0 alpha.

      I wish I could copy in my source, but I'm at work now and not on the box where I labored at this problem. In any case, I think I meticulously followed the spec, JBoss instructions and every other many to one example I could find.

      The table gets created ok by JBoss. Even the foreign key field. I then create a root EJB, followed by some children using an create method that takes as one arguments the properties plus the parent EJBLocalObject. I call setParent(parentRef) in the ejbPostCreate of the child.

      I would expect to have seen the database foreign key get populated with the parent's key, but it remains NULL. The logs show the insert statement for the properties, but no update statement for the parent field. The relationship does get created in memory because I can traverse it, but it doesn't get persisted.

      I'm assuming either of two things: 1) there is a bug or 2) I'm missing something about populating the foreign key.

        • 1. Re: CMP 2.0 not populating foreign key field for many to one
          F Zemen Newbie

          If anyone was interested, turns out it was because the insert contained a blob. When I removed that field, the foreign key was properly updated.

          • 2. Re: CMP 2.0 not populating foreign key field for many to one
            kleku Newbie

            Hi,
            I think it's a bug (quite serious)
            3.2.1, mysql, connector 3.0.8

            these are code fragments from ProductPriceEjb bean:

            public Long ejbCreate(ProductLocal product, Price price) throws CreateException {
            setValue(price.getPrice().getValue());//just a BigDecimal value (pk is auto-incremented)
            return null;
            }

            public void ejbPostCreate(ProductLocal product, Price price) {
            if (product == null) {//just to be sure
            throw new EJBException();
            }
            setProduct(product);
            }


            public void ejbStore() {
            System.out.println("Store: " + getProduct().getPrimaryKey());
            //throws NPE but only if create is called more than once (after second create)
            }

            Bye

            • 3. Re: CMP 2.0 not populating foreign key field for many to one
              kleku Newbie

              I just forgot to mention - no blobs

              • 4. Re: CMP 2.0 not populating foreign key field for many to one
                Thomas Hüsler Newbie

                Hi all

                I am actually experiencing the same problem. (find detailed scenario description at end of mail).

                When I was debugging I figured out that the sql insert call is already executed after ejbCreate and not after ejbPostCreate. When then looking into the sql param / statement I saw that it contains my foreign key field in the statement but of course the param value is not set yet...

                When I place a breakpoint in ejbPostCreate it even never holds (now this does not mean too much, but get worried about that)...

                I would really appreciate any help for that. At the moment I get the pk key value from the passed in parent obj reference and set it already in ejb create and again set in post create the reference itself...

                BTW I am not using any blobs or anything...

                Please help!
                Tom

                Scenario:
                - Table Person (PK int, Name, first)
                - Table Contract (PK int, PersonFK int)
                - Relationship between contract and person (bidirectional) 1 Person may have many contracts
                - PersonFK in Table contract has a not null criteria defined in db
                - IBM DB2 running on Mainframe
                - JBoss 3.2.1 used

                • 5. Re: CMP 2.0 not populating foreign key field for many to one
                  viz Newbie

                  I think there is a bug: JBoss 3.2.1, mySQL, 3.0.8 connector.

                  I had two tables (Customer and Address) both using AUTO_INCREMENT. Although rows were created JBoss seemed unable to create the relationship between them (1-2-1).

                  I went back to took a look at the Customer bean and discovered that JBoss does not seem to like AUTO_INCREMENT on the PK. If I call create() - without PK as this is AUTO_INCREMENT - the row is created, but try remove() (or findByPrimaryKey(...) and JBoss throws an error:

                  java.lang.IllegalArgumentException: Attempt to get lock ref with a null object

                  Now, I'm not sure if this is related?

                  Any help gratefully received.
                  --
                  Marc

                  • 6. Re: CMP 2.0 not populating foreign key field for many to one
                    sreram Newbie

                    I am facing the same problem in jboss 3.2.1 version. I have a master table. this table has some set of attributes(CMR) which needs to be inserted in the detail table(related table). I am getting a "dulicate column name " error. but the entity is getting created for master table.

                    I tried with <foreign-key-mapping /> tag.
                    I tried with <relation-table-mappting > tag.
                    I tried with tag.

                    nothing seems to work. I suspect it is either bug or descriptor is wrong.

                    have any tried to insert master-detail table with CMR using JBoss. if so please suggest me what is wrong.