Rick,

Using the broker api, I've just built a very similar relationship and
got it working. If I paste in my code you should be able to just change
the class names to match your app. 

I have a one-to-many relationship between a Contact object and an object
called SimpleProperty. SimplePropery holds a foreign key pointing back
to a contact, so one Contact can hold many SimplePropertys but a
SimplePropery always belongs to one Contact. 

First setup your mapping XML so that the contact object knows how to
populate its collection. (I'm using MS SQL so the key type is varchar in
my code). The two classes are simple Java Beans.
 

<class-descriptor
          class="com.designuk.crm.Contact"
          table="contacts"
   >
      <field-descriptor
         name="guid"
         column="guid"
         jdbc-type="VARCHAR"
         primarykey="true"
         autoincrement="true"
         nullable="false"
      />

        ... some other properties ...

<collection-descriptor
         name="simpleProperties"
         element-class-ref="com.designuk.crm.SimpleProperty"
      >
         <inverse-foreignkey field-ref="ownerGuid"/>
      </collection-descriptor> 
            
      
   </class-descriptor>


with ownerGuid being the foreign key in simpleProperties

<class-descriptor
          class="com.designuk.crm.SimpleProperty"
          table="simpleProperties"
   >
      <field-descriptor
         name="guid"
         column="guid"
         jdbc-type="VARCHAR"
         primarykey="true"
         autoincrement="true"
         nullable="false"
      />

        ... other properties ...

      <field-descriptor
         name="ownerGuid"
         column="ownerGuid"
         nullable="false"
         jdbc-type="VARCHAR"
      />
</class-descriptor>



This mapping setup means that every time you query the database via one
of the APIs to get a Contact, it will automatically populate the correct
SimpleProperty objects into contact's simpleProperties collection.
(Pretty cool...)


To query across these two objects, you can build criteria. For example
to get all the Contacts that have a SimplePropery with
name="nationality" AND value="British" (name and value being a fields in
the simpleProperties table):

// get persistence broker
PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();


// build two criteria on simpleProperties table

Criteria c1 = new Criteria();
c1.addEqualTo("simpleProperties.name", "nationality");
Criteria c2 = new Criteria();
c2.addEqualTo("simpleProperties.value", "British");

// AND the two criteria together
c1.addAndCriteria(c2);
    
Query jquery = QueryFactory.newQuery(Contact.class, c1);

Collection jresults = broker.getCollectionByQuery(jquery);


This gives you a collection of Contact objects matching your query, each
one with the appropriate number of SimpleProperty objects loaded into
its Collection.

Hope this helps, Please correct me if I've made any erroneous
assumptions. 


Brendan.


-----Original Message-----
From: Rick Banerjee [mailto:[EMAIL PROTECTED] 
Sent: 30 January 2004 06:22
To: [EMAIL PROTECTED]
Subject: Simple 1:n mapping question

Hi Everybody,

I have a 1:n relationship between 
Person & Application tables.

I have Person & Application valueobjects
representing the tables respectively.

I want to search on the join of these
tables with criteria pertaining to both
tables. 

In the Person value object I have a
Collection allApplications to hold applications
for a Person.

Is there any way to get a limited set of Applications
into the allApplications collection?

Regards

Rick


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to