I hardly have any idea how to proceed when deleting.
My problem is that if a category is associated to an issue, and I try to delete it from project, I should not be able to do that.
How can I do that? Help please.
I have 3 tables, Issue, Project, Category
The relationships are as follows:
1. A project may have many issues, an issue is related to only one project
2. An issue may have only one category
3. A project may have one or many categories
Issue.hbm.xml is as follows:
<id name="id" type="Int32" unsaved-value="0" access="field">
<column name="ID" length="4" sql-type="int" not-null="true" unique="true" index="PK_Issue"/>
<generator class="native" />
</id>
<many-to-one name="Project" class="API.Project, API">
<column name="ProjectID" length="4" sql-type="int" not-null="false"/>
</many-to-one>
<many-to-one name="Category" class="API.Category, API">
<column name="CategoryID" length="4" sql-type="int" not-null="false"/>
</many-to-one>
project.hbm.xml
<id name="id" type="Int32" unsaved-value="0" access="field">
<column name="ID" length="4" sql-type="int" not-null="true" unique="true" index="PK_Project"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="Name" length="200" sql-type="varchar" not-null="true" unique="true" index="IX_Project_Name"/>
</property>
category.hbm.xml
<id name="id" type="Int32" unsaved-value="0" access="field">
<column name="ID" sql-type="int" not-null="true" unique="true" index="PK_Category"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="Name" length="50" sql-type="varchar" not-null="true" unique="true" index="IX_Category"/>
</property>
<many-to-one name="Project" class="API.Project, API" >
<column name="ProjectID" length="4" sql-type="int" not-null="false"/>
</many-to-one>
I am assuming in your database you have foreign keys for the referenced ID’s? (if not you should do). In which case the foreign-key attribute in your mapping is required, and currently it looks like it is missing.
e.g. for issue.hbm.xml it may look something like this:
The FK names I’ve put in here are just assumptions, go and check out what they are in your DB to make sure they are correct.
Once NHibernate knows about your FK relationships it should take care of the rest 🙂
EDIT:
OOPs – the foreign-key property should be in the many-to-one element not the column sorry. See amended code.