在 Hibernate/NHibernate 中映射多对多而无需连接表

发布于 2024-08-17 20:01:43 字数 553 浏览 4 评论 0原文

我有两个表:

CalendarEntry
  Id
  Date
  ...

Holiday
  Id
  Date
  ...

在我的 CalendarEntry 类中,我有一个这样的属性

public ISet<Holiday> Holidays { ... }

,我想将其关联到同一 Date 发生的 Holiday 实例code> 作为 CalendarEntry。但是,我想不出如何做到这一点。

我尝试将其映射为一对多,但一对多自动假定它应该使用 CalendarEntryId 列执行联接(大概是因为它是唯一保证唯一的属性,它必须是一对多)。

我尝试将其映射为多对多,但似乎多对多需要一个单独的联接表,在本例中我不想要它。

我的问题是:是否可以在 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 技术交流群。

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

发布评论

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

评论(3

哎呦我呸! 2024-08-24 20:01:43

我认为您需要做的是设置一个查询来获取假期条目。我不确定这是否可以使用映射来完成。


我原来的答案中的附加信息可能不适用:您可以在 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.

你是我的挚爱i 2024-08-24 20:01:43

您应该能够使用 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."

冰魂雪魄 2024-08-24 20:01:43

您可以创建一个数据库视图(或派生表查询),其中包含来自 CalendarEntryHoliday 的相应 ID,然后使用该视图映射多对多关系 - 从技术上讲,这是避免使用连接表,同时允许 NHibernate 按其想要的方式工作。

您需要确保 NHibernate 不会尝试更新集合(可能是 inverse="true"cascade="none" 的某种组合)并避免修改它们在代码中 - 我想你对此没有意见,因为你根本不想要一张桌子。

You could create a database view (or derived table query) containing the appropriate ids from CalendarEntry and Holiday, 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"and cascade="none") and avoid modifying the them in code - I presume you're okay with this given you don't want a table at all.

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