流利的休眠和SQLite 映射组件
我的实体上有一个可能为空的组件。这适用于 InMemory 数据库的单元测试,但不适用于基于文件的 SQLite 数据库。 我使用布尔标志来指示组件是否已设置,但这似乎是一种黑客行为。
这是 NHibernate 还是 SQLite 错误?或者我错过了什么?
以下是我的映射,去掉了业务价值:
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();
}
}
对于内存数据库,当实体的所有列都为空时,实体上的组件也为空。这就是我所期望的。 当使用基于文件的数据库时,该组件为空。所以我无法使用 entity.Component==null
来检查组件是否已设置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用内存测试了您的代码,它按预期返回内存数据库的
Component != null
。在NHibernate中,集合永远不会为空,但可以为空,并且只有当所有属性都为空时,组件才为空,因此组件永远不应该为空。我猜想您测试后的Get 返回的实例与 Component 设置为 null 的实例相同。
Get 返回已加载的实例,该实例的 Component 设置为非空。
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 likeGet returns the same instance which has Component set to null.
Get returns the loaded instance which has Component set to non-null.