5 Replies Latest reply on Apr 23, 2003 6:49 PM by tommyg

    JBoss3.2:same name for CMR & CMP fields

    san121

      Dear friends,

      Form my experience with jboss3.0.6 & mySQL i found out that it is not possible to give same field name for a CMR and CMP field. This gives me duplicate coulmn names while creating a table or while inserting a new record.The only solution is to give different column names for these fields, which is not possible in my case as i am not 'allowed' to change the DB schema.

      I'll be immensly thankful if somebod can help me out this regard. Is this true that Jboss will not allow same name for CMR & CMP fields? Is this problem solved in Version 3.2?

      Thanks in Advance & best wishes

        • 1. Re: JBoss3.2:same name for CMR & CMP fields
          tommyg

          I was able to get this to work.

          • 2. Re: JBoss3.2:same name for CMR & CMP fields
            san121

            Dear tommyg,
            Its so very nice 2 hear that u solved it! Cud u please post details of your solution.
            Thanks

            • 3. Re: JBoss3.2:same name for CMR & CMP fields
              tommyg

              The solution is to describe the setter and getter for the field that holds the foreign key value, and also in your ejb-jar.xml file describe the relationship and how that field is used in the relationship. Basically set up the relationship as you normally would in both the bean and the ejb-jar file AND in your CMP bean write the setter and getter fields.

              If you need information on setting the relationship I would recommend using Xdoclet and try the example.

              • 4. Re: JBoss3.2:same name for CMR & CMP fields
                san121

                Thanks for the comments.
                I am already having setter/getter methods for the field that holds the foreign key value, which is anyway needed because this field should also act as a CMP field. So what am i missing?

                I assume that i am defining the relations correctly, bcoz i am able to get the bean work if the CMP field and CMR field are having different names (and hence two different sets of settter/getter methods)

                I am getting into problem only when
                ( A) I make the name of CMP and CMR fields SAME
                AND
                (B) when i Perform an INSERT operation (Kindly note that other finder methods will not make problem here. only ejbCreate() will cause problem)

                • 5. Re: JBoss3.2:same name for CMR & CMP fields
                  tommyg



                  I'm using Xdoclet and JBoss 3.2 RC4. I'll just describe the relevant parts of my files.

                  The 'one' class is called PersonIdentification and it has a relationship with a 'many' class called PersonSubject.

                  Both the one class and the many class have a column called PERSON_ID. PERSON_ID is the primary key in the 'one' table and the foreign key in the 'many' table. Both beans have a get and set for personId.

                  Parts of 4 files are described below: 1) part of the 'one' class 2) part of the many class 3) part of the session bean 4) part of the ejb-jar file.

                  Hope this helps.


                  1.
                  This is part of the 'one' class and describes the many relationship
                  /**
                  * @param personSubject for this person
                  *
                  * @ejb:interface-method
                  */
                  public abstract void setPersonSubject(java.util.Collection personSubject);
                  /**
                  * @return all subjects for this person
                  *
                  * @ejb:interface-method view-type="local"
                  * @ejb:relation
                  * name="person-personSubject"
                  * role-name="one-person-has-many-subjects"
                  * target-role-name="many-subjects-belong-to-one-person"
                  * target-ejb="PersonSubject"
                  * target-multiple="no"
                  * target-cascade-delete="yes"
                  *
                  * @jboss:target-relation related-pk-field="personId"
                  * fk-column="PERSON_ID"
                  */
                  public abstract java.util.Collection getPersonSubject();

                  2.
                  The following shows the ejbCreate method for the 'many' object. Notice I set the foreign key value.
                  /**
                  * Create a person subject.
                  * @ejb:create-method
                  */
                  public PersonSubjectPK ejbCreate( PersonSubjectData psd )
                  throws CreateException {
                  setSubjectId( psd.getSubjectId() );
                  setPersonId( psd.getPersonId() );
                  return null;
                  }

                  3.
                  In my session bean I call the create method for PersonSubject.
                  PersonSubjectLocal p = home.create(psd);

                  4. this is the part of the ejb-jar file that describes the relationship.

                  <!-- Relationships -->

                  <ejb-relation >
                  <ejb-relation-name>person-personSubject</ejb-relation-name>

                  <ejb-relationship-role >
                  <ejb-relationship-role-name>one-person-has-many-subjects</ejb-relationship-role-name>
                  One
                  <relationship-role-source >
                  <ejb-name>PersonIdentification</ejb-name>
                  </relationship-role-source>
                  <cmr-field >
                  <cmr-field-name>personSubject</cmr-field-name>
                  <cmr-field-type>java.util.Collection</cmr-field-type>
                  </cmr-field>
                  </ejb-relationship-role>

                  <ejb-relationship-role >
                  <ejb-relationship-role-name>many-subjects-belong-to-one-person</ejb-relationship-role-name>
                  Many
                  <cascade-delete/>
                  <relationship-role-source >
                  <ejb-name>PersonSubject</ejb-name>
                  </relationship-role-source>
                  </ejb-relationship-role>

                  </ejb-relation>