Ok a different error, but still similar. I made the changes you recommended.
A.hbm.xml
Code:
<composite-id name="compId" class="A_PK">
<key-property name="column1" column="C1"/>
<key-property name="column2" column="C2" />
<key-property name="column3" column="C3"/>
</composite-id>
<properties name="keysNeededByB">
<property name="column2" column="C2" insert="false" update="false"/>
<property name="column3" column="C3" nsert="false" update="false"/>
</properties>
<bag name="relatedMappings">
<key property-ref="keysNeededByB"/>
<many-to-many class="B">
<column name="C2"/>
<column name="C3" />
</many-to-many>
</bag>
B.hbm.xml
Code:
<composite-id>
<key-property name="C2" column="C2" />
<key-property name="C3" column="C3" />
<key-property name="C4" column="C4" />
<key-property name="C5" column="C5" />
</composite-id>
<bag name="relatedAs">
<key>
<column name="C2"/>
<column name="C3" />
</key>
<many-to-many class="A">
<column name="C2"/>
<column name="C3" />
</many-to-many>
</bag>
I was unable to reuse the property-ref in they key of B's bag because it said the property was not found, so I just put in the key columns by hand. I hope that's ok.
I get the following error
Foreign key (FKBBC59F5BC559B2B7:relatedAs [C2, C3])) must have same number of columns as the referenced primary key (B [C2, C3, C4, C5])
This is fishy. Why would B's set be referencing the primary key of B? As you can see from the mapping files, B's set (relatedAs) maps to instances of A. So I'm not sure what that means. The mappings make sense to me. I'm defining two sets and in both cases, I map 2 columns on one end to 2 columns on the other. Hibernate insists on matching the columns to the PK. :(
In the example 23.4.3, it looks similar (I tried to make mine look like theirs), but it looks like they use 'formula' to make the number of columns in the many-to-many match the number in the PK. I wasn't able to find very good documentation about what formula does in the Hibernate docs, so maybe there's a solution in their somewhere.
Otherwise, please let me know what's wrong with my mappings. Thanks.