NHibernate:在不同服务器上的多个数据库之间映射实体,无需数据库链接

发布于 2024-10-20 22:07:54 字数 207 浏览 2 评论 0原文

使用最新版本的NHibernate(可能还有一些插件),是否可以在不使用数据库链接的情况下在不同服务器上的多个数据库之间映射实体?

对于背景,我希望实现与DBA中描述的内容大致相似的东西.SE帖子

Using the latest version of NHibernate (and possibly some plugins), is that possible to map entities across multiple databases on different servers without DB link?

For the background, I'm looking to implement something loosely similar to what is described in this DBA.SE post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夏了南城 2024-10-27 22:07:54

我在以下问题上得到了有趣的答案 nhusers 邮件列表。摘录如下:

来自 Jason Meckley

[...] 如果您正在谈论存储
跨多个实体的单个实体
数据库,答案并不明确。
你也许可以做一些事情
自定义用户类型和/或事件
听众给物体水合,但是
维护起来会很混乱。

另一种方法是转移
使用从一个数据库到另一个数据库的数据
ETL 过程。类似于
复制,但仅传输
其他数据库所需的数据。
你最终得到 1 个可写数据库
许多人阅读数据库。然后你映射
域到单个数据库。

另一个选择是确定原因
有多个数据库。如果他们
代表不同类型的
然后明确表达数据/对象
这在域中。例如
CustomerWithAddress 是不同的
实体而不是 CustomerOrder。如果您需要
来自两者的信息,然后查询每个
分别建立数据库并建立
代码中的投影。

var x =
session1.get(id);
变量y =
session2.get(id);
返回合并(x,y);

I've got an interesting answer on the nhusers mailing list. Excerpt follows:

From Jason Meckley

[...] if you are talking about storing
a single entity across multiple
databases, the answer is not cleanly.
you might be able to do something with
custom user types and/or event
listeners to hydrate the object, but
that would be a mess to maintain.

A different approach is to transfer
the data from one db to another using
an ETL process. similar to
replication, but only transferring the
data required by the other database.
you end up with 1 writable database
and many read databases. then you map
the domain to a single database.

Another option is to determine why
there are multiple databases. if they
represent different types of
data/objects then explicitly express
this in the domain. for example
CustomerWithAddress is a different
entity than CustomerOrder. If you need
information from both, then query each
database individually and build up a
projection in code.

var x =
session1.get(id);
var y =
session2.get(id);
return merge(x, y);

七月上 2024-10-27 22:07:54

经过一些谷歌搜索(虽然我还没有尝试过),您显然至少可以使用

<class name="..." table="..." schema="database.schema">
  ...
</class>

映射将类映射到同一服务器上不同数据库中的表。

但恕我直言,这是一个蹩脚的解决方案,因为它似乎并不独立于服务器。显然,NHibernate 只是在创建查询时将模式值与表值连接起来 - 对于大多数服务器来说,“只需将数据库与模式放在一起”就会产生一种语法,这种语法会起作用。但是你并没有清楚地告诉NHibernate“这是模式”和“这是数据库”,以便NHibernate可以清楚地决定如何构建查询——你通过在不属于的地方注入上下文数据来搞乱它的查询创建。

恕我直言,NHibernate 应该抑制这种肮脏的黑客行为,并在“schema”值包含 .

无论如何,似乎没有技术原因说明您不能更进一步并使用 schema="server.database.schema",其中“server”是链接服务器的名称(例如,请参见 http://technet.microsoft.com/de-de/library/ms190479.aspx )

除了那个肮脏的黑客之外,每个人似乎都建议使用多个会话工厂。

After some googeling (although I haven't tried this yet), you can apparently at least map classes to tables in different databases on the same server, using

<class name="..." table="..." schema="database.schema">
  ...
</class>

in the mapping.

But IMHO this is a crappy solution, because it doesn't seem to be server-independent. Apparently, NHibernate just concatenates the schema value with the table value in the creation of the query - and for most servers, "just putting the database in there with the schema" results in a syntax, which HAPPENS work. BUT you don't cleanly tell NHibernate "this is the schema" and "this is the database" so that NHibernate can cleanly decide how to build the query - you mess with its query creation by injecting contextual data where it doesn't belong.

IMHO, NHibernate should suppress such dirty hacks and throw an exception if the "schema" value contains a .

Anyhow, there seems to be no technical reason why you shouldn't be able to go one step further and use schema="server.database.schema", where "server" is a name of a linked server (see for example http://technet.microsoft.com/de-de/library/ms190479.aspx )

Apart from that dirty hack, everyone seems to recommend using multiple session factories.

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