views:

61

answers:

2

I am having the following in my .hbm.xml file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                                    namespace="Core.Domain.Model"
                                    assembly="Core">

  <class name="Category" table="Categories" dynamic-update="true">
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="Guid">
      <generator class="guid"/>
    </id>
    <property name="Name" length="100">
    <column name="Name" unique="true" index="IX_Category"/>
    </property>
  </class>
</hibernate-mapping>

I am having the following code, however, I can see that no exception is being raised when I am inserting a duplicate value for the Name field. Why is that so?

 void IRepository<Category>.Save(Category entity)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(entity);
                    transaction.Commit();
                }
            }
            scope.Complete();
            }    
        }
+2  A: 

Did NHibernate generate the database schema for you? Because even though you set the property as unique, if the schema does not have the unique constraint, it will do nothing. Basically, that property won't query the database looking for uniqueness; it will only set the SQL constraint on insert.

Rafael Belliard
Thanks, I have modified the database but now i`m having the error message Violation of UNIQUE KEY constraint 'UQ__Categories__0EA330E9'. Cannot insert duplicate key in object 'dbo.Categories'. The statement has been terminated, however, I would prefer to have the error message generated by NHibernate,(e.InnerException.Message i.e something like Message = "Cannot insert duplicate key row in object 'dbo.Categories' with unique index 'IX_Category'.\r\nThe statement has been terminated."...I would prefer nHibernate to generate the throw the exception message
Like JoeBilly said, look into NHibernate Validator. You can then make NHibernate check for uniqueness and throw whatever you want.
Rafael Belliard
+3  A: 

The unique attribute is only used when generating a DDL schema from a mapping (hbm).

If you want to check uniqueness with NHibernate, it is data validation and you should look NHibernate validator. And as Rafael said you have to write your own validator.

You can also check the natural-id element.

Unless you absolutely need to validate uniqueness on application side, this sample and this one make me think it may be better to let the database do this job.

JoeBilly
Noting that even with the excellent NHibernate Validator, you would have to write your own validation, because it obviously does not have an [Unique] validator (which would require a hit to the database).
Rafael Belliard
True, edited to include that it may be only a good practice if the validation have absolutely to be done on the persisted data.
JoeBilly