Been working for too long on trying to set up a uni-directional one-to-many container managed relationship that will persist the relationship using a database foreign key. Hoping to get some help from the JUG.

I am using J2EE Reference Implementation version 1.3.1. My search for good sample code has turned up very few examples that explain uni-directional one-to-many relationships, even though I expect that this is a very common type of relationship. The best example of one-to-many relationships I can find is the Parts-Suppliers example from the SunOne App Server. There are also some good examples at www.caucho.com, but they have proprietary extensions in their container that allow you to specify a SQL table and column  - this appears to do exactly what I need in J2EE, but can't find how to accomplish it.

In any case, I have lots of working code, but it doesn't behave as expected. I have JSPs communicating with a Stateless Session Bean communicating with two CMP2.0 Entity Beans. Backend Database is MySql.

My application involves 2 very simple database tables with container managed persistent fields:

1) evalform table (An evaluation form containing questions):

CREATE TABLE evalformBeanTable
(   evalform_id VARCHAR(8) PRIMARY KEY,
    evalform_name VARCHAR(30),
);

2) evalformquestion table (The questions related to the evaluation form):

CREATE TABLE evalformquestionBeanTable
(   evalformquestion_id VARCHAR(17) PRIMARY KEY,
    evalform VARCHAR(8),              [This is the FOREIGN KEY that refers to evalform PK]
    evalformquestion_question_id VARCHAR(8),
    evalformquestion_text VARCHAR(120),
);

The problem is this:

J2EE RI doesn't seem to recognize that I want to have the relationship persisted via the FOREIGN key in the evalformquestion table. Instead, J2EE is creating a SEPARATE database table to store the relationship data.

Is there a way I can tell J2EE to use the foreign key, rather than creating a new table?

Below is deployment descriptor for the relationship. I realize that this really indicates a BI-directional, rather than uni-directional relationship. However, it seems I really need both CMR fields, even though I think of the relationship as uni-directional:
- need the collection side so that I can request all the questions related to the evalform
 - also need the String CMR field for the evalform foreign key - otherwise how will the container know which database field contains the FK?

<relationships>
    <ejb-relation>

        <ejb-relationship-name>Evalform-EvalformQuestion</ejb-relationship-name>

        <ejb-relationship-role>
            <ejb-relationship-role-name>Evalform-has-EvalformQuestions</ejb-relationship-role-name>
            <multiplicity>One</multiplicity>
            <relationship-role-source><ejb-name>EvalformEJB</ejb-name></relationship-role-source>

                <cmr-field>
                    <cmr-field-name>evalformquestions</cmr-field-name>
                    <cmr-field-type>java.util.Collection</cmr-field-type>
                </cmr-field>

        </ejb-relationship-role>

        <ejb-relationship-role>
            <ejb-relationship-role-name>EvalformQuestions-belong-to-Evalform</ejb-relationship-role-name>
            <multiplicity>Many</multiplicity>
            <cascade-delete/>
            <relationship-role-source><ejb-name>EvalformQuestionEJB</ejb-name></relationship-role-source>

                <cmr-field>
                    <cmr-field-name>evalform</cmr-field-name>
                </cmr-field>

        </ejb-relationship-role>

    </ejb-relation>
</relationships>

Any help is appreciated.

Thanks, Hugh

_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to