I was finally able to get the following many-to-many query to work using the included mapping file:
 
Query:
"select i from " + HelpItemServiceType.class.getName() + " i where i.items.type = $1"
 
Mapping:
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
 <!--Mapping for HelpItem-->
 <class name="HelpItem" auto-complete="false" identity="ID" access="shared" key-generator="SEQUENCE">
  <map-to table="help_item"/>
  <field name="ID" type="integer">
   <sql name="item_key" type="numeric"/>
  </field>
  <field name="type" type="java.lang.String">
   <sql name="help_type" type="char"/>
  </field>
  <field name="services" collection="collection" type="HelpItemServiceType">
   <sql name="service_code" many-table="help_item_service" many-key="item_key"/>
  </field>
 </class>
 <!--Mapping for HelpItemServiceType-->
 <class name="HelpItemServiceType" auto-complete="false" identity="code" access="shared">
  <map-to table="services"/>
  <field name="code" type="java.lang.String">
   <sql name="service_code" type="varchar"/>
  </field>
  <field name="description" type="java.lang.String">
   <sql name="service_desc" type="varchar"/>
  </field>
  <field name="items" collection="collection" type="HelpItem">
   <sql name="item_key" many-table="help_item_service" many-key="service_code"/>
  </field>
 </class>
</mapping>
My mapping table in the database has the following columns: "item_key" and "service_code"
 
One thing strikes me as odd though. Why must I set the "name" attribute of the "sql" element for the collections to the opposite key in the mapping table?  In looking at the method ParseTreeWalker.addJoinsForPathExpression() where the joins for a many-to-many relationship are created, I can't decide if there is a bug there or if it's supposed to work that way.
 
Shannon Kendrick

Reply via email to