> > > The following issues apply to foreign-key-mapping ..
> > > 
> > > 
> > > 1) problems with unidirectional relations:
> > > 
> > > deployment does not work, because the 
> > > JDBCCMRFieldBridge.initRelatedData 
> > > does not find the related cmrField -> throws DeploymentException
> > 
> > I just fixed a bug in this code last night, where the 
> self-relationships
> > would not initialize properly. Try the newest code.
> > 
> 
> Can't say that in the moment, because I made them 
> bidirectional, now. I'm 
> currently stuck with the third problem ..

This fix was for a relationship from a bean back to it self, so I don't
think  it will fix anything you were seeing befor.  Then again this was most
likely a sideaffet of the third issue.
 
> > > 2) problems with "not null" constraints on foreign keys:
> > > 
> > > the JDBCCreateEntityCommand builds an insert statement with 
> > > the cmp fields 
> > > but without the cmr fields .. as far as I know, createEntity 
> > > is the only 
> > > affected command
> > 
> > I don't think this is correct.  Are you trying to create an 
> EJB that has a
> > foreign key relationship to another bean that is guaranteed 
> not be null.
> > The only way I can think of to do this is to set the cmr 
> fiend in the
> > ejbCreate method, which would give the fk a value during 
> the initial sql
> > insert statement.  Unfortunately, it is not legal to set a 
> cmr field during
> > the ejbCreate method; You have to wait until ejbPostCreate. Did I
> > misinterpret you?
> >
> 
> I think, you are right .. should have read the spec more 
> deeply .. but 
> this restriction implies, that foreign keys must never have a 
> "NOT NULL" 
> constraint. I wonder, if that was an intended behaviour?!

I think this was added to allow vendors with shitty persistence engines to
be EJB 2.0 compliant.

> > > 3) problems with <foreign-key-fields> in jbosscmp:
> > > 
> > > the <field-name>s are searched for in the related entity during
> > > JDBCRelationshipRoleMetaData.loadForeignKeyFields but it 
> > > seems to be also 
> > > used as the name of the cmr field's foreign key.
> > 
> > Ok, I'm a little lost. I don't remember how the default 
> name for a fk is
> > constructed, but I remember basing it on related object's 
> pk field name.  I
> > will add some documentation how all the default name are created.
> >
> 
> The default names don't apply, because I use the jbosscmp 
> descriptor to 
> override them (the default is <field-name>_id)
> 
> 
> > > I assume, the specified name should be that of the foreign 
> > > key, rather 
> > > than that of the related primary key ..
> > 
> > Specified where?
> >
> 
> .. in the <field-name> of a <foreign-key-mapping> in the jbosscmp 
> descriptor.
> 
> >
> > > It could also mean, that I should have specified the opposite 
> > > relation 
> > > ship role in the jbosscmp descriptor .. but this might have no 
> > > <ejb-relationship-role-name>, if the relationship is 
> unidirectional!
> > 
> > I don't know. Can you post the important parts of the ejb-jar and
> > jbosscmp-jdbc files?
> >
> 
> Here you are
> ------------
> 
> tables:
> 
> -- version : attribute = 1 : n
> 
> CREATE TABLE egor.Version
> (
>   id            BIGINT    NOT NULL,
>   number        BIGINT    NOT NULL,
>   CONSTRAINT PK_Version PRIMARY KEY (id)
> );
> 
> CREATE TABLE egor.Attribute
> (
>   id            BIGINT    NOT NULL,
>   version       BIGINT    CONSTRAINT attribute-belongsto-version
>                           REFERENCES egor.Version(id),
>   value         CLOB(2K),
>   CONSTRAINT PK_Attribute PRIMARY KEY (id)
> );
> 
> 
> beans:
> 
> public abstract class VersionBean implements EntityBean
> {
>     // ...
> 
>     /**
>      * @ejb:interface-method view-type="local"
>      */
>     public abstract void setAttributes(Collection attributes);
>     /**
>      * @ejb:interface-method view-type="local"
>      * @ejb:relation name="version-attribute" 
>                      role-name="version-has-attributes" 
> multiple="false"
>      */
>     public abstract Collection getAttributes();
> }
> 
> public abstract class AttributeBean implements EntityBean
> {
>     // ...
> 
>     /**
>      * @ejb:interface-method view-type="local"
>      */
>     public abstract void setVersion(LocalVersion version);
>     /**
>      * @ejb:interface-method view-type="local"
>      * @ejb:relation name="version-attribute" 
>                      role-name="attribute-belongsto-version"
>      */
>     public abstract LocalVersion getVersion();
> }
> 
> 
> ejb-jar:
> 
>     <relationships>
>       <ejb-relation>
>          <ejb-relation-name>version-attribute</ejb-relation-name>
>          <ejb-relationship-role>
>             
> <ejb-relationship-role-name>version-has-attributes</ejb-relati
> onship-role-name>
>             <multiplicity>One</multiplicity>
>             <relationship-role-source>
>                <ejb-name>Version</ejb-name>
>             </relationship-role-source>
>             <cmr-field>
>                <cmr-field-name>attributes</cmr-field-name>
>                <cmr-field-type>java.util.Collection</cmr-field-type>
>             </cmr-field>
>          </ejb-relationship-role>
> 
>          <ejb-relationship-role>
>             
> <ejb-relationship-role-name>attribute-belongsto-version</ejb-r
> elationship-role-name>
>             <multiplicity>Many</multiplicity>
>             <relationship-role-source>
>                <ejb-name>Attribute</ejb-name>
>             </relationship-role-source>
>             <cmr-field>
>                <cmr-field-name>version</cmr-field-name>
>             </cmr-field>
>          </ejb-relationship-role>
>       </ejb-relation>
>     </relationships>         
> 
> jbosscmp:
> 
>   <relationships>
>     <ejb-relation>
>       <ejb-relation-name>entry-version</ejb-relation-name>
>       <foreign-key-mapping>
>         <ejb-relationship-role>
>           
> <ejb-relationship-role-name>version-belongsto-entry</ejb-relat
> ionship-role-name>
>           <foreign-key-fields>
>             <foreign-key-field>
>               <field-name>version</field-name>
>               <column-name>version</column-name>
>             </foreign-key-field>
>           </foreign-key-fields>
>         </ejb-relationship-role>
>       </foreign-key-mapping>
>     </ejb-relation>
>   </relationships>
> 
> 
> 
> Problem:
> 
> this doesn't deploy but complains:
> 
>    CMP field for foreign key not found: field name=version
> 
> however, it _does_ deploy, if the I replace:
>               <field-name>version</field-name>   (1)
> by:
>               <field-name>id</field-name>        (2)
> 
> The field "version" or "id" in the latter case is searched 
> for in the  
> VersionBean. So please tell me, what is actually correct (1 
> or 2). (1) 
> does'nt deploy, (2) fails during JDBCFindByForeignKeyCommand.
> 
> If we agree on this, I can tell you the Exception, debug output, 
> whatever you need, perhaps fix it myself
> 
> 
> hope, that helps ..
> 
> Holger
> 

In the jbosscmr-jdbc.xml you need to specify both sides of the relationship.
it should be something like this:

<ejb-relation>
   <ejb-relation-name>AB_OneToMany_Bi_FK</ejb-relation-name>
   <foreign-key-mapping>
      <ejb-relationship-role>
         <ejb-relationship-role-name>A-has-Bs</ejb-relationship-role-name>

         <!-- this side doesn't have any keys -->
         <foreign-key-fields/>  
      </ejb-relationship-role>

      <ejb-relationship-role>
 
<ejb-relationship-role-name>B-belongsto-A</ejb-relationship-role-name>
         <foreign-key-fields>
            <foreign-key-field>
               <field-name>a_cmp_pk_field_name</field-name>
               <column-name>A_ID_PK</column-name>
            </foreign-key-field>
         </foreign-key-fields>
      </ejb-relationship-role>
   </foreign-key-mapping>
</ejb-relation>

So for your code it should be something like this:

<ejb-relation>
   <ejb-relation-name>entry-version</ejb-relation-name>
   <foreign-key-mapping>
      <ejb-relationship-role>
 
<ejb-relationship-role-name>version-attribute</ejb-relationship-role-name>

         <!-- this side doesn't have any keys -->
         <foreign-key-fields/>  
      </ejb-relationship-role>

      <ejb-relationship-role>
 
<ejb-relationship-role-name>attribute-belongsto-version</ejb-relationship-ro
le-name>
         <foreign-key-fields>
            <foreign-key-field>
               <field-name>id</field-name> <!-- Version.id -->
               <column-name>version</column-name> <!-- maps to
Attribute.version -->
            </foreign-key-field>
         </foreign-key-fields>
      </ejb-relationship-role>
   </foreign-key-mapping>
</ejb-relation>

The field-name is equivlent to the SQL REFERANCES clause.  That should fix
your problems.  I need to add more error checking.  The system should have
bitched that the field-name you specified was not a pk field of Version.

Dain

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to