在 Hibernate/NHibernate 中映射多对多而无需连接表
我有两个表:
CalendarEntry
Id
Date
...
Holiday
Id
Date
...
在我的 CalendarEntry
类中,我有一个这样的属性
public ISet<Holiday> Holidays { ... }
,我想将其关联到同一 Date
发生的 Holiday
实例code> 作为 CalendarEntry
。但是,我想不出如何做到这一点。
我尝试将其映射为一对多,但一对多自动假定它应该使用 CalendarEntry
的 Id
列执行联接(大概是因为它是唯一保证唯一的属性,它必须是一对多)。
我尝试将其映射为多对多,但似乎多对多需要一个单独的联接表,在本例中我不想要它。
我的问题是:是否可以在 NHibernate 中映射它,我应该怎么做?如果不可能,为什么?
I have two tables:
CalendarEntry
Id
Date
...
Holiday
Id
Date
...
In my CalendarEntry
class, I have a property like this
public ISet<Holiday> Holidays { ... }
which I want to associate to the Holiday
instances that occur on the same Date
as the CalendarEntry
. However, I can't come up with how to do this.
I've tried mapping it as one-to-many, but one-to-many automatically assumes that it should perform the join using CalendarEntry
's Id
column (presumably since it's the only property that is guaranteed to be unique, which it must be to be one-to-many).
I've tried mapping it as many-to-many, but it seems that many-to-many requires a separate join table, which I don't want in this case.
My question is: is it possible to map this in NHibernate, and how should I do it? If it's not possible, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要做的是设置一个查询来获取假期条目。我不确定这是否可以使用映射来完成。
我原来的答案中的附加信息可能不适用:您可以在 NHibernate 中拥有多对多关系,而无需为连接表条目创建单独的实体类,但基础数据仍然需要存在于数据库中的某个位置。
I think what you need to do is set up a query to get the Holiday entries. I'm not sure this can be done using a mapping.
Additional info from my original answer, which may not apply: You can have the many-to-many relationship in NHibernate without creating a separate entity class for the join table entries, but the underlying data still needs to exist somewhere in the database.
您应该能够使用 property-ref 来完成此操作,我相信 NHibernate 2.1 中提供了该功能。我在 链接中找到了它text,这里是用于将其添加到 NHibernate 的 jira。
我不确定这是否适用于日期列,如果您的日期字段包含时间数据,您肯定不走运。
您始终可以将关系排除在模型之外,并通过存储库方法(即 GetHolidaysForDate)访问集合。这对我来说更有意义,因为假期与日历条目和特定文化不同。除非您使用相当于“假期”的非美国含义。
You should be able to do this using a property-ref, which I believe is available in NHibernate 2.1. I found it in the link text and here's the jira for adding it to NHibernate.
I'm not sure this would work on a date column and you're definitely out of luck if your date field contains time data.
You can always leave the relationship out of the model and access the collection through a repository method, i.e. GetHolidaysForDate. This would make more sense to me since holidays are distinct from calendar entries and culture specific. Unless you're using the non-US meaning equivalent to "vacation."
您可以创建一个数据库视图(或派生表查询),其中包含来自
CalendarEntry
和Holiday
的相应 ID,然后使用该视图映射多对多关系 - 从技术上讲,这是避免使用连接表,同时允许 NHibernate 按其想要的方式工作。您需要确保 NHibernate 不会尝试更新集合(可能是
inverse="true"
和cascade="none"
的某种组合)并避免修改它们在代码中 - 我想你对此没有意见,因为你根本不想要一张桌子。You could create a database view (or derived table query) containing the appropriate ids from
CalendarEntry
andHoliday
, then map a many-to-many relationship using the view - this technically avoids having a join table, while allowing NHibernate to work the way it wants to.You'll want to make sure NHibernate doesn't attempt to update the collections (probably some combination of
inverse="true"
andcascade="none"
) and avoid modifying the them in code - I presume you're okay with this given you don't want a table at all.