nhibernate 中可以从基类类型更改为连接子类类型吗?
我有文档扫描系统,可以扫描多种类型的文档。最初,文档在扫描时没有任何信息,然后对它们进行分类,并在稍后的第二步中为其输入附加信息。因此,我有一个名为 Document 的基类,以及每种类型的子类及其各自的元数据,如下所示。我将它设置为 NHibernate 中的每个子类表(连接的子类)映射。
public class Document
{
public int ID { get; set; }
public string FilePath { get; set; }
}
public class Certificate : Document
{
// certificate-specific fields
}
public class Correspondence : Document
{
// correspondence-specific fields
}
我需要做的是首先创建一个 Document 类并保存它。然后在稍后的第二步中检索并将其转换为子类类型之一并填写其其余信息。做到这一点的最佳方法是什么?NHibernate 是否有可能做到这一点?如果可能的话,我想保留原始文件记录,但如果我不得不放弃它,也不会破坏交易。
I have document scanning system where several types of documents are scanned. Initially, the document has no information when its scanned, then they get classified and additional information is entered for them in a second step later. So, I have a base class called Document, and subclasses for each type with their respective metadata like below. I have it setup as a table-per-subclass (joined subclass) mapping in NHibernate.
public class Document
{
public int ID { get; set; }
public string FilePath { get; set; }
}
public class Certificate : Document
{
// certificate-specific fields
}
public class Correspondence : Document
{
// correspondence-specific fields
}
What I need to be able to do is create a Document class first and save it. Then retrieve in a second step later on and convert it to one of the subclass types and fill in the rest of its information. What would be the best approach to do this, and is this even possible with NHibernate? If at all possible I would like to retain the original document record, but its not a dealbreaker if I have to jettison it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,NHibernate 不允许您在初始创建后在子类之间进行切换;要使其按照您想要的方式工作,您有 3 个选项:
Unfortunately, NHibernate does not allow you to switch between subclasses after initial creation; to get this working the way you want, you have 3 options:
这个问题已经讨论过 这里。我会采纳 Terry Wilcox 的建议,为此使用一个角色。组合优于继承。
This issue has already been discussed here. I would go with Terry Wilcox's tip to use a role for this. Composition over inheritance.