Domanda

I have a component on an entity that may be null. This works in a unit test with an InMemory database, but does not work on a file based SQLite database. I use a boolean flag to indicate whether the component was set, but this seems to be a hack.

Is this a NHibernate or a SQLite Bug? Or am i missing something?

Here are my mappings stripped of business value:

public sealed class EntityMap : ClassMap<Entity>
{
   public EntityMap()
   {
     Not.LazyLoad();
     Id(m => m.Uid);
     Map(m => m.HasComponent).Not.Nullable();
     Component(m => m.Component).ColumnPrefix("Component");
   }
}

public sealed class MyComponentMap : ComponentMap<MyComponent>
{
   public MyComponentMap()
   {
     Map(c => c.SomeBasicProperty).Nullable();
     HasMany(c => c.ListOfValues).AsList(li => li.Column("Number"))
                           .Component(part => 
                                         {
                                             part.Map(s => s.Name);
                                             part.Map(s => s.Value);
                                         }
                            .Table("ComponentValues")
                            .Cascade.AllDeleteOrphan().Not.LazyLoad();

   }
}

With the memory database the component on the entity is null when all columns for it are null. This is what i expect. When using a file based database the component is empty. So i cannot use entity.Component==null to check if the Component has already been set.

È stato utile?

Soluzione

i tested your code with inmemory and it returns Component != null for an inmemory database as expected. In NHibernate a collection is never null but can be empty and a component is only null if all properties are null hence the component should never be null. i guess that you tested like

using (var tx = session.BeginTransaction())
{
    id = session.Save(new Entity());
    tx.Commit();
}

var entity = session.Get<Entity>(id);
Console.WriteLine(entity.Component == null);

Get returns the same instance which has Component set to null.

using (var tx = session.BeginTransaction())
{
    id = session.Save(new Entity());
    tx.Commit();
}

session.Clear();

var entity = session.Get<Entity>(id);
Console.WriteLine(entity.Component == null);

Get returns the loaded instance which has Component set to non-null.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top