tags:

views:

115

answers:

2

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>
A: 

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:

<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" foreign-key="FK_Issue_Project"> 
  <column name="ProjectID" length="4" sql-type="int" not-null="false"/> 
</many-to-one> 
<many-to-one name="Category" class="API.Category, API" foreign-key="FK_Issue_Category"> 
  <column name="CategoryID" length="4" sql-type="int" not-null="false" /> 
</many-to-one>

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.

Scozzard
Hi Thanks for replying. I`m having the error message: {"API.NHibernate.Issue.hbm.xml(9,75): XML validation error: The 'foreign-key' attribute is not declared."}In my DB, I can see the following under Foreign Key Relationships:FK_Issue_Category, FK_Issue_Project and I made the changes according from your example.
Have edited code, take a look and let me know how it goes :)
Scozzard
A: 

IMO, if you shouldn't be able to do something, the classes shouldn't allow it.

You didn't provide the code of your classes. However it looks like, make sure that only valid operations are possible on the business objects. With NH, you are using an ORM. The key of your application is not the database, it is your class model. If you don't really use the power of object orientation, you don't get the full power of NH.

Most probably, encapsulation is the solution here.

This said, the database itself should always provide some minimal consistency check as well. You should probably make the ProjectID of the category not null. (sometimes NH gets problems with the not null constraints, for instance when there are circular references. Then you need to remove them).

Off topic: usually you don't need to specify the sql-types. They are set by NH to meaningful defaults. And: you don't need to write the database schema yourself (unless you are on a legacy database). You can use the (highly recommended) ExportSchema class which writes the schema according to your mapping files. Then you don't need to write the same information twice.

Stefan Steinegger