I have two entities, report and rule. Each report is connected to zero or more rules and each rule is connected to zero or more reports.
I choose to map the entities as lists, because the order of the rules connected to each report is important.
In my Report.hbm.xml, I mapped
<!-- Each report is connected to zero or more rules -->
<!-- we have a n:m relationship here -->
<!-- ordering is important -->
<list name="rules" table="APP_REPORT_RULE" lazy="true" cascade="none">
<key column="ID_REPORT"/>
<index column="SORTING"/>
<many-to-many column="ID_RULE" class="itoskop.bo.Rule"/>
</list>
And in my Rule.hbm.xml I mapped:
<!-- m:n relationship reports -->
<list name="reports" table="APP_REPORT_RULE" lazy="true" cascade="none" inverse="true">
<key column="ID_RULE"/>
<index column="SORTING"/>
<many-to-many column="ID_REPORT" class="itoskop.bo.Report"/>
</list>
When I run hbm2ddl, I get (partly) this SQL:
create table APP_REPORT_RULE (
ID_REPORT INTEGER not null,
ID_RULE INTEGER not null,
SORTING INTEGER not null,
primary key (ID_RULE, SORTING)
);
which is not exactly what I expected, because this prevents me from associating one rule to more than one report on the same index position.
If I had modelled the schema by hand, the primary key would have been more like:
...
primary key (ID_RULE, SORTING)
...
What is wrong with my mapping (since it is not really m:n)? Or can't I do that with lists?
Andy
|