NHibernate:一个基类,几个映射 [英] NHibernate: One base class, several mappings

查看:21
本文介绍了NHibernate:一个基类,几个映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 NHibernate 比较陌生,但是最近几个程序一直在使用它,我爱上了它.我遇到了一种情况,我需要将 4-5 个数据库中的数据聚合到一个数据库中.具体是序列号数据.每个数据库都会有自己的映射文件,但最终实体都共享相同的基本结构(Serial 类).

I'm relatively new to NHibernate, but have been using it for the last few programs and I'm in love. I've come to a situation where I need to aggregate data from 4-5 databases into a single database. Specifically it is serial number data. Each database will have its own mapping file, but ultimately the entities all share the same basic structure (Serial class).

我知道 NHibernate 需要每个类的映射,所以我最初的想法是有一个基本的 Serial 类,然后为每个不同的数据库继承它并创建一个唯一的映射文件(继承的类将具有零内容).这对于获取所有数据和填充对象应该非常有用.然后我想做的是使用基类映射将这些继承的类(不确定正确的术语是什么)保存到基类表中.

I understand NHibernate wants a mapping per class, and so my initial thought was to have a base Serial Class and then inherit from it for each different database and create a unique mapping file (the inherited class would have zero content). This should work great for grabbing all the data and populating the objects. What I would then like to do is save these inherited classes (not sure what the proper term is) to the base class table using the base class mapping.

问题是我不知道如何强制 NHIbernate 为对象使用特定的映射文件.使用session.save()"时,将继承的类转换为基类没有任何作用(它抱怨没有映射).

The problem is I have no idea how to force NHIbernate to use a specific mapping file for an object. Casting the inherited class to the base class does nothing when using 'session.save()' (it complains of no mapping).

有没有办法明确指定要使用的映射?或者我是否缺少一些 OOP 主体来更具体地将继承的类转换为基类?或者这个想法只是个坏主意.

Is there a way to explicitly specify which mapping to use? Or is there just some OOP principal I am missing to more specifically cast an inherited class to base class? Or is this idea just a bad one.

我能找到的关于 NHibernate(第 8 章)的所有继承内容似乎并不完全适用于这个函数,但我可能是错的(每个具体类的表看起来可能有用,但是关于 NHibernate 如何确定要做什么,我无法完全理解它.

All of the inheritance stuff I could find with regards to NHibernate (Chapter 8) doesn't seem to be totally applicable to this function, but I could be wrong (the table-per-concrete-class looks maybe useful, but I can't wrap my head around it totally with regards to how NHibernate figures out what to do).

推荐答案

我不知道这是否会有所帮助,但基本上我不会尝试这样做.

I don't know if this'll help, but I wouldn't be trying to do that, basically.

从本质上讲,我认为您可能患有金锤"综合症:当您拥有一个非常棒的锤子(即 Hibernate(我同意您对此的看法;它是一个很棒的工具))时,一切看起来都像钉子.

Essentially, I think you're possibly suffering from "golder hammer" syndrome: when you have a REALLY REALLY nice hammer (i.e. Hibernate (and I share your opinion on it; it's a MAGNIFICENT tool)), everything looks like a nail.

我通常会尝试简单地拥有一个手动转换"类,即具有构造函数的类,这些构造函数为您的各个串行类采用休眠类,并将数据简单地复制到其自己的特定格式;然后 Hibernate 可以使用自己的映射简单地将其序列化到(单个)数据库.

I'd generally try to simply have a "manual conversion" class, i.e. one which has constructors which take the hibernate classes for your individual Serial Classes and which simply copies the data over to its own specific format; then Hibernate can simply serialize it to the (single) database using its own mapping.

实际上,我认为这是一个更好的解决方案的原因是,您正在有效地尝试在类中进行非对称序列化;即从派生类中的一个数据库读取,写入基类中的另一个数据库.没有什么太可怕的,真的,除了它基本上是一个单向过程;如果您真的想从一个数据库转换到另一个数据库,只需进行转换,然后就可以了.

Effectively, the reason why I think this is a better solution is that what you're effectively trying to do is have asymmetric serialization in your class; i.e. read from one database in your derived class, write to another database in your base class. Nothing too horrible about that, really, except that it's fundamentally a unidirectional process; if you really want conversion from one database to the other, simply do the conversion, and be over with it.

这篇关于NHibernate:一个基类,几个映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆