NHibernate 使用 Fluent 引用子类映射
我尝试了许多不同的映射配置,但继续生成异常或创建/更新错误的记录。
例外
- 无法在表中为标识列插入显式值
- 无法将标识列键生成与联合子类映射一起使用
- 为 XXX 类生成 null id
当我有一个确实保存的映射时,我遇到以下问题
我已成功在数据库中插入和更新记录,但这些记录没有适当的 ID。它们的 id 都为 0,因此只会一遍又一遍地更新相同的记录。
我试图解决的问题
我正在尝试SubclassMap
接口IRequest
。此接口用作单独类 AbstractWorkflowRequestInformation
上的属性。保存父类时,我想将引用的IRequest
保存在适当的子类表中。这是我当前的映射,它生成异常无法在表中插入标识列的显式值
。我确信我在映射这两个类之间的关系的方式上进行了一些调整。我做错了什么?我的地图如下。
IRequestMap
public class IRequestMap : ClassMap<IRequest>
{
public IRequestMap()
{
Id(x => x.WorkflowRequestInformation)
.GeneratedBy.Foreign("AbstractWorkflowRequestInformation");
UseUnionSubclassForInheritanceMapping();
}
}
public class PlanRequestMap : SubclassMap<PlanRequest>
{
public PlanRequestMap()
{
Table("plan_request");
// specific to PlanRequest property mappings and references
}
}
public class BnjRequestMap : SubclassMap<BnjRequest>
{
public BnjRequestMap()
{
Table("scratchdb.guest.bnj_request");
// specific to BnjRequest property mappings and references
}
}
AbstractWorkflowRequestInformationMap
public class AbstractWorkflowRequestInformationMap :
ClassMap<AbstractWorkflowRequestInformation>
{
public AbstractWorkflowRequestInformationMap()
{
Table("workflow_request_information");
Id(x => x.Id)
.Column("workflow_request_information_id")
.GeneratedBy.Identity();
// more property mappings and references
References(x => x.SuperDuperRequest, "workflow_request_information_id")
.Class<IRequest>().Unique().Cascade.All();
// more property mappings and references
}
}
I have tried many different mapping configurations but continue to generate exceptions or create / update the wrong record.
Exceptions
- Cannot insert explicit value for identity column in table
- Cannot use identity column key generation with union-subclass mapping
- null id generated for class XXX
When I have a map that does save I have the following problem
I have successfully inserted and updated records in the database but these records do not have the appropriate id. They all have 0 for an id and as such only update the same record over and over.
Problem I am trying to solve
I am attempting to SubclassMap
the interface IRequest
. This interface is used as a property on a separate class AbstractWorkflowRequestInformation
. When saving the parent class I want to save the referenced IRequest
in the appropriate sub-class table. This is my current mapping which generates the exception Cannot insert explicit value for identity column in table
. I am sure I have something tweaked in the way I am mapping the relationship between these two classes. What am I doing wrong? My maps are below.
IRequestMap
public class IRequestMap : ClassMap<IRequest>
{
public IRequestMap()
{
Id(x => x.WorkflowRequestInformation)
.GeneratedBy.Foreign("AbstractWorkflowRequestInformation");
UseUnionSubclassForInheritanceMapping();
}
}
public class PlanRequestMap : SubclassMap<PlanRequest>
{
public PlanRequestMap()
{
Table("plan_request");
// specific to PlanRequest property mappings and references
}
}
public class BnjRequestMap : SubclassMap<BnjRequest>
{
public BnjRequestMap()
{
Table("scratchdb.guest.bnj_request");
// specific to BnjRequest property mappings and references
}
}
AbstractWorkflowRequestInformationMap
public class AbstractWorkflowRequestInformationMap :
ClassMap<AbstractWorkflowRequestInformation>
{
public AbstractWorkflowRequestInformationMap()
{
Table("workflow_request_information");
Id(x => x.Id)
.Column("workflow_request_information_id")
.GeneratedBy.Identity();
// more property mappings and references
References(x => x.SuperDuperRequest, "workflow_request_information_id")
.Class<IRequest>().Unique().Cascade.All();
// more property mappings and references
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是我的 IRequest 映射缺少对 AbstractWorkflowRequestInformation 的约束引用。因此,通过添加 WorkflowRequestInformation 属性和约束,现在一切都按我的预期工作。
The issue here was that my IRequest map was missing a constraining reference back to AbstractWorkflowRequestInformation. So by adding a WorkflowRequestInformation property and the constraint everything now works as I intended.