0

I am following the Summer of NHibernate tutorials but I am not using the xml mappings but instead, I am making use of Fluent NHibernate to do the mappings.

My Customer entity class is:

public class Customer
{
    public virtual int CustomerId { get; set; }

    public virtual string Firstname { get; set; }

    public virtual string Lastname { get; set; }   
}

The corresponding mapping class is:

public class CustomerMap: ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x =>x.CustomerId);
        Map(x => x.Firstname).Length(50).Nullable();
        Map(x => x.Lastname).Length(50).Nullable();
        ImportType<CustomerFirstnameCounter>();
    }

}

My DAO class is:

 public int AddCustomer( Customer customer )
    {

        using( ISession session = GetSession() )
        {
            using( ITransaction tx = session.BeginTransaction() )
            {
                try
                {
                    int newId = ( int ) session.Save( customer );
                    session.Flush();
                    tx.Commit();
                    return newId;
                }
                catch( GenericADOException )
                {
                    tx.Rollback();
                    throw;
                }
            }
        }

    }

And finally my test is:

 [Test]
 public void AddCustomerThrowsExceptionOnFail()
 {
       // Arrange
       Customer customer = BuildInvalidCustomer();

       // Act
       _provider.AddCustomer( customer );

       // Assert

 }

When the test runs, no exception is thrown! So my first question is whether anyone can see what is wrong with my mapping.

Now, in the dB, the Firstname field is set as a varchar(50). When I debug the test, I see that the data is inserted but truncated (I do get warning messages). So this might indicate
that I haven't set the dB up properly. Can anyone point me in the direction of where to prevent this truncation of data in SQL Server?

2 Answers 2

1

This answer should help you. I will also use Data Annotation StringLengthAttribute to ensure validation of your properties

3
  • Thanks Iridio. I felt that Cole W's answer was more precise. But your answer will help someone else hopefully.
    – DavidS
    Commented Oct 31, 2011 at 21:14
  • In fact, I'm after the opposite. I actually do NOT want data which exceeds a certain length to be inserted into the dB in a truncated format.
    – DavidS
    Commented Nov 1, 2011 at 9:46
  • Well with DataAnnotation or something similar as a concept you can avoid the save to the DB
    – Iridio
    Commented Nov 1, 2011 at 15:58
0

.Length(50) does not check lengths at run-time. This is only used if you are generating the database schema from the mappings.

If you wish to validate the length of values you will have to either do this manually or use some validation framework like NHibernate Validator

3
  • But tell me shouldn't the dB prevent me from performing such operations i.e. truncating data but still inserting it?
    – DavidS
    Commented Nov 1, 2011 at 12:22
  • If you are using MSSQL you should look to see if ANSI_WARNINGS are turned on or off. If they are turned ON and column length is exceeded it will return an error and roll back the insert. msdn.microsoft.com/en-us/library/ms190368.aspx
    – Cole W
    Commented Nov 1, 2011 at 12:29
  • I am using MSSQL Express 2008 and ANSI WARNING is set to true but there is no transaction rollback.
    – DavidS
    Commented Nov 1, 2011 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.