Hi.
I and my colleague have been struggling to get the mapping for our
legacy db right for so long that it has been decided to kindly ask for
help here :)

say you have following classes
-------
 public class Child
{
        public virtual int Id { get; set; }
        public virtual Parent MyParent { get; set; }
}
public class Parent
{
        public virtual int Id { get; set; }
}


..that should be mapped to following db:
--------
create table Parent (
      Id INT IDENTITY(1,1) NOT NULL,
      primary key (Id)
   )

   create table Child (
      Id INT IDENTITY(1,1) NOT NULL,
      ParentId INT DEFAULT(0) NOT NULL,
      primary key (Id)
  )
--------------


Note that Child.ParentId is defined with a default value - we have
LOTS of columns defined like this. The idea behind having 0 as a
default value is to "mimick" DBNull because DBNull was not support by
the legacy system we have. The same approach has been taken to define
varchar columns, for example "Foo VarChar DEFAULT('') NOT NULL"
We got an IUsetType (MyCustomType) implementation for that column
which basically translates NULL to 0 and vice versa.

here's the mapping:
-----------------------------------
  <class name="Parent" table="Parent">
    <id name="Id" column="Id" type="TestCustomType.MyCustomType,
TestCustomType">
      <generator class="native" />
    </id>
  </class>

  <class name="Child" table="Child">
    <id name="Id" column="Id" type="Int32">
      <generator class="native" />
    </id>
    <many-to-one name="MyParent" class="Parent" column="ParentId"
cascade="save-update"/>
  </class>
-----------------------------------

Here is the test I want to pass:

        [Test]
        public void Can_select_child_where_Parent_is_null()
        {
            using (var tx = _session.BeginTransaction())
            {
                _session.Save(new Child());
                tx.Commit();
            }
            var criteria = _session.CreateCriteria<Child>();
            criteria.Add(Restrictions.IsNull("MyParent"));
            var child = criteria.List<Child>();
            Assert.That(child.Count, Is.GreaterThan(0));
        }

generated query is
SELECT this_.Id as Id1_0_, this_.ParentId as ParentId1_0_ FROM Child
this_ WHERE this_.ParentId is null

and I would like to get something like
SELECT this_.Id as Id1_0_, this_.ParentId as ParentId1_0_ FROM Child
this_ WHERE this_.ParentId=0

Of course NULL to 0 translation can be (and it was) handled in
MyParent property getter/setter, which forced us to have
Child.ParentId _property_ etc. But I have a feeling that there must be
some better way to do this...
Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to