用Fluent nHibernate映射到多个表 [英] Mapping to multiple tables with Fluent nHibernate
问题描述
这是我的情况。
假设你有以下模型实体,它们自己代表单个表格:$ b $bâ€
电影 [Movie_Id,Title,Rating,..]
Actors [Actor_Id,FirstName,LastName,..]
导演 [Director_Id,FirstName,LastName,..]
另一个名为Recommendations的实体/表格表示用户一个网站。这个想法是推荐可以是任何类型的,即有人推荐一个演员,或某人推荐一部电影。基本上,这个表应该是这样的:
...
建议 [Recommendation_Id,Object_Id,Object_Type,..]
这里是我陷入了困境。如何将这些关系映射到nHibernate中与Fluent?
我的意思是,在映射时,我不能指定类型(与哪个表相关),因为这是在运行时确定的,但是我不能仅仅依靠Id本身,因为它不能暗示哪个
例如,想象一下在Recommendations表上的记录:
Recommendation_Id - Object_Id - Object_Type
b $ b 83001 --4010123 ---M
基本上我存储一个字符标识符(在这种情况下,M代表表电影),以知道Object_Id属于哪个表至。作为最终的评论,我想补充一点,我已经看到了所有的每班表格,table-per-subclass,table-per-concrete-class的例子,但是我相信这些都不适合这种情况,因为Movies_Id,Actors_Id,Directors_Id,...都是不同的,Recommendations_Id也是如此。我的意思是,在这里没有基类的子类继承,他们根本不共享Id。
我希望我自己清楚。
在此先感谢。
您正在寻找的NHibernate映射是
< any />
。这里有一些资源可以帮助你加快这个NHibernate映射功能: - Ayende的博文 NHibernate文档 com / jagregory / fluent-nhibernate / wiki / Fluent-mapping#anyrel =noreferrer>流利的NHibernate文档 b
< class name =推荐表格=建议>
< id name =Id>
< column name =Recommendation_Id/>
< generator class =native/>
< / id>
< any name =RecommendedObjectid-type =System.Int32meta-type =System.String>
< meta-value value =Mclass =Movie/>
< meta-value value =Aclass =Actor/>
< meta-value value =Dclass =Director/>
< column name =Object_Type/>
< column name =Object_Id/>
< / any>
<! - 其他东西... - >
< / class>
你应该可以用Fluent NHibernate来完成这个工作,就像在推荐的映射中一样:
ReferencesAny(x => x.RecommendedObject)
.IdentityType< int>()
.EntityTypeColumn(Object_Type )
.EntityIdentifierColumn(Object_Id)
.AddMetaValue< Movie>(M)
.AddMetaValue< Actor>(A)
.AddMetaValue< Director>( d);
Here's my situation..
Suppose you have the following model entities, which represent single tables on their own:
Movies [ Movie_Id, Title, Rating, .. ]
Actors [ Actor_Id, FirstName, LastName, .. ]
Director [ Director_Id, FirstName, LastName, .. ]
And another entity/table called "Recommendations", which represents recommendations between users within a website. The idea is that a recommendation could be of any type, i.e. someone recommending an actor, or someone recommending a movie. Basically, the table should look something like this:
Recommendations [ Recommendation_Id, Object_Id, Object_Type, .. ]
And here's what i'm stuck into. How can I map these relationships in nHibernate with Fluent? I mean.. while mapping, I can't specify the Type (which table related to) 'cause that's determined on runtime, but I can't rely only on the Id 'cause by itself it cannot imply to which table belongs to.
For instance, imagine this record on Recommendations table:
Recommendation_Id--Object_Id--Object_Type
83001--4010123---"M"
Basically I'm storing a char identifier (in this case "M" stands for table "Movies") to know to which table Object_Id belongs to. I cannot just store Object_Id without Object_Type..As final comments, I'd like to add that I've seen all the table-per-class, table-per-subclass, table-per-concrete-class examples, but I believe none of these fits in this situation, as Movies_Id, Actors_Id, Directors_Id, ... are all different between them, so is Recommendations_Id. I mean, there's no base class-child class inheritance here, they don't share Id's at all..
I hope I make myself clear. Thanks in advance.
解决方案The NHibernate mapping you are looking for is
<any/>
. Here are a few resources to help you get up to speed with this NHibernate mapping feature:I believe the *.hbm.xml that you are shooting for is something like this:
<class name="Recommendation" table="Recommendations"> <id name="Id"> <column name="Recommendation_Id" /> <generator class="native"/> </id> <any name="RecommendedObject" id-type="System.Int32" meta-type="System.String"> <meta-value value="M" class="Movie"/> <meta-value value="A" class="Actor"/> <meta-value value="D" class="Director"/> <column name="Object_Type"/> <column name="Object_Id"/> </any> <!-- other stuff ... --> </class>
You should be able to accomplish that with Fluent NHibernate like so in Recommendation's mapping:
ReferencesAny(x => x.RecommendedObject) .IdentityType<int>() .EntityTypeColumn("Object_Type") .EntityIdentifierColumn("Object_Id") .AddMetaValue<Movie>("M") .AddMetaValue<Actor>("A") .AddMetaValue<Director>("D");
这篇关于用Fluent nHibernate映射到多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!