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

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

问题描述

我是比较新的NHibernate的,但用它在过去的几个节目已经和我恋爱。我来的情况下,我需要从4-5数据库中的数据汇总到一个数据库。具体地说,它是序列号的数据。每个数据库都会有自己的映射文件,但最终的实体都共享相同的基本结构(串行类)。

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的希望每个类的映射,所以我最初的想法是有一个基地串行类,然后从它继承为每个不同的数据库,并创建一个独特的映射文件(继承类将有为零的内容)。这应该抓住所有的数据和填充物工作的伟大。那么,什么我想要做的就是这些继承类(不知道正确的术语是什么)保存到使用基类映射基础类表。

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.

从本质上讲,我觉得你可能是从戈尔德锤子综合症:当你有一个非常非常好的锤子(即休眠(我在上面分享你的意见,这是一个宏伟的工具)),一切看起来都像钉子。

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类为您单独的序列构造类和它简单地拷贝到其自己的特定格式的数据;那么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天全站免登陆