nhibernate 中可以从基类类型更改为连接子类类型吗?

发布于 2024-09-24 09:00:18 字数 575 浏览 0 评论 0原文

我有文档扫描系统,可以扫描多种类型的文档。最初,文档在扫描时没有任何信息,然后对它们进行分类,并在稍后的第二步中为其输入附加信息。因此,我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倦话 2024-10-01 09:00:18

不幸的是,NHibernate 不允许您在初始创建后在子类之间进行切换;要使其按照您想要的方式工作,您有 3 个选项:

  1. 使用本机 sql 调用来更改鉴别器(并且可能)添加或更改任何与子类相关的字段。
  2. 将对象的内容复制到适当类的新对象,然后删除原始对象。
  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:

  1. Use a native sql call to change the discriminator (and possibly) add or change any subclass-related fields.
  2. Copy the contents of your object to a new object of the proper class and then delete the original.
  3. Don't use subclasses, control the state of your object through an enumeration or some other mechanism that allows you to determine their type at run-time.
如此安好 2024-10-01 09:00:18

这个问题已经讨论过 这里。我会采纳 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文