A have a couple of issues with a relatively simple data model:
Code:
<class name="DomainModel.Method, DomainModel" table="Method">
...
<bag name="Steps" inverse="true" cascade="all-delete-orphan" order-by="Sequence">
<key column="MethodId"/>
<one-to-many class="DomainModel.MethodStep, DomainModel" />
</bag>
</class>
<class name="DomainModel. MethodStep, DomainModel" table="MethodStep">
...
<property name="Sequence" column="Sequence" type="Int32"/>
<many-to-one name="Method" column=" MethodId " class="DomainModel.Method, DomainModel"/>
<many-to-one name="Step" column="StepId" class="DomainModel.Step, DomainModel"/>
</class>
<class name="DomainModel.Step, DomainModel" table="Step">
...
<property name="StepType" column="StepType" type="String"/>
<bag name="MethodSteps" inverse="true" lazy="true">
<key column="StepId"/>
<one-to-many class="DomainModel.MethodStep, DomainModel"/>
</bag>
</class>
A many to many relationship with a join table that also has a Sequence column that is used to order the Method.Steps collection.
All tables have a surrogate primary key which is guids generated by NHibernate.
My problems are:
1. I work on the Method entity and create, delete and reorder MethodSteps. The cascade attribute makes sure that additions or removal from the collections are persisted. However, when I do a reorder that is lost because the collections are marked as inverse. Is there any way to have it both ways? I tried to remove the inverse=true statement but that does not work well with the all-delete-orphan option.
2. Method should ideally have two collections instead of just the Steps collection. I would like something similar to:
Code:
<bag name="PreSteps" inverse="true" cascade="all-delete-orphan" order-by="Sequence">
<key column="MethodId"/>
<one-to-many class="DomainModel.MethodStep, DomainModel" />
</bag>
<bag name="PostSteps" inverse="true" cascade="all-delete-orphan" order-by="Sequence">
<key column="MethodId"/>
<one-to-many class="DomainModel.MethodStep, DomainModel" />
</bag>
The Step entity has a StepType column which can be either 'Pre' or 'Post'. I would like to have a restriction on the two collections so they only have the 'Pre' steps or the 'Post' steps.
I have tried to add a "where" attribute to the collection statements but I am having trouble creating a valid statement since the aliases used by the query engine changes sometimes. Sometimes "step0_.StepType='Pre'" will work but at other times is the alias for Step step1_. Is there anyway to let NHibernate know that I refer to the Step entity and let the query engine replace it with the right alias?
Is there any other way to have the two collections?
Thanks for your time,
HakonB