next up previous contents
Next: Adding constraints Up: Storing in the Database Previous: Complex Storage and Heavy   Contents

Many 2 Many

Handling many to many relationships between two classes(each with its own table) requires a third table to store the relationship. The code generator will handle this and provide strings to handle inserting a relationship as well as lookups each way(So for User to Group a lookup getting all the Users in a Group, or all the Groups for a given User).
 <class name="${user.pkg}.UserToGroup" 
      many-to-many="true" 
      factory-ext="ManyToManyFactoryBase"
      >
    <map-to table="merlin_user_desk"/>

  <field name="user" type="${user.pkg}.User"
     >
    <sql name="user_oid"/>
  </field>
  <field name="desk" type="${user.pkg}.Group">
	<sql name="desk_oid"/>
  </field>
 </class>

Note: the field many-to-many was set to true : and the base factory was set to ManyToManyFactoryBase.



2002-03-05