简体   繁体   中英

Fluent NHibernate: mapping property “Length” ignored when inserting or updating

we are trying to (slowly) migrate all our programs from Delphi to C#, and its our first time with an ORM, so please forgive if the question may be silly. ;-)

We are using:

  • DB2 Version 9.7.5
  • FluentNHibernate Version 1.3.0.0
  • NHibernate Version 3.3.1.4000

For our unit test project, I got the entity "TestCSharp" defined. This type has a string property named "TextVar". Here is the mapping:

public class TestCsharpMapping:ClassMap<TestCsharp>
{
  /// <summary> 
  /// map constructor
  /// </summary>
  public TestCsharpMapping()     
  {
     Schema("TEST");
     Table("TEST_CSHARP");
     base.Id(x => x.Id).Column("ID").GeneratedBy.Sequence("ID_SEQUENCE");
     Map(x => x.TextVar).Column("TEXT_VAR").Nullable().Length(20);
  }

}

The table TEST.TEST_CSHARP resides on a linux DB2 database, the field TEXT_VAR is a VARCHAR(20) field.

This is my unit test (the hibernate session itself is encapsulated by a wrapper type we are designing, so dont you wonder):

  [TestMethod]
  public void TestEditTextFields()
  {
     ReInitSession();
     CleanUp();
     var testobjects = _dataProviderSession.LoadAll<TestCsharp>(QueryDuration.ShortQuery);
     testobjects.Add( new TestCsharp());
     // overflow : Text_Var is only 20 chars long
     testobjects[0].TextVar = "01234567890123456789A";
     _dataProviderSession.Save();
     testobjects = _dataProviderSession.LoadAll<TestCsharp>(QueryDuration.ShortQuery);
     Assert.AreEqual(1, testobjects.Count);
     Assert.AreEqual("01234567890123456789", testobjects[0].TextVar);
  }

The unit test is failing at the Save() command with a DB2 error "IBM.Data.DB2.DB2Exception: ERROR [22001] [IBM] CLI0109E Zeichenfolge rechts abgeschnitten." (which is german for "string right truncated"). Assigning a string value with a length less or equal to 20 characters leads to no exception.

So what is it I am missing? The Length property doesn´t seem to have any functional impact. I was assuming that the call "Length(20)" would cause the ORM to cut the string down to 20 characters when saving (just as Borlands Delphi did when setting the size of field components inside datasets). :-/

Any hints are welcome!

Kind regards

Andreas

Your assumption is incorrect. NHibernate will not truncate the string for you, which does not seem like a good idea anyway, as you are silently losing data.

Setting Length in the mapping will just set the database field length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM