NHibernate - 我有很多,但我只想要一个!

发布于 2024-08-28 09:06:54 字数 1299 浏览 9 评论 0原文

我有一个可以有很多电子邮件的用户。这是通过列表集合(由用户上的 IEnumerable 电子邮件公开)进行映射的。 对于每个用户,其中一封电子邮件将是主要电子邮件(电子邮件上的“Boolean IsPrimary”属性)。

如何在 NHibernate 不加载用户的每封电子邮件的情况下从用户获取主要电子邮件?

我有以下两个实体,每个实体都有一个相应的表

public class User
{
   private readonly IList<Email> _emails;

   public virtual int Id { get; private set; }
   public virtual IEnumerable<Email> Emails { get { return _emails; } }
   // public virtual Email PrimaryEmail { get; set; } - Possible somehow ?
}
public class Email
{
   public virtual int Id { get; private set; }
   public virtual String Address { get; set; }
   public virtual Boolean IsPrimary { get; set; }
   public virtual User User { get; set; }
}

我可以将用户上的“Email PrimaryEmail”属性等映射到以某种方式设置“IsPrimary=1”的电子邮件吗? 也许使用 Sql 公式?一个视图?一对一的关系 ?或者另一种方式?

应该可以将主电子邮件更改为其他电子邮件之一,因此我想将它们全部保留在 1 个表中,只需更改 IsPrimary 属性。

使用 Sql 公式,如果我将当前主电子邮件的 IsPrimary 属性设置为 false,然后将 PrimaryEmail 属性设置为要更新的电子邮件,是否可以使用户的“PrimaryEmail”属性保持最新状态应该是新的主要电子邮件并将 IsPrimary 设置为 true ? NHibernate 会跟踪 Sql 公式加载的“旧/当前”主电子邮件的更改吗? 使用 SqlFormula 时,一级缓存和二级缓存怎么样?

我不知道它是否可以通过使用 View 来工作?那么我猜电子邮件可以像组件一样映射?从视图加载时更新电子邮件数据时它会起作用吗?

有更好的办法吗?

由于我在用户和电子邮件之间具有双向关系,因此在许多情况下我当然可以查询主电子邮件,然后使用电子邮件上的“用户”属性来获取用户(而不是相反 - 从用户到主要电子邮件)

希望有人可以提供帮助?

I have a User which can have many Emails. This is mapped through a List collection (exposed by IEnumerable Emails on the User).
For each User one of the Emails will be the Primary one ("Boolean IsPrimary" property on Email).

How can I get the primary Email from User without NHibernate loads every email for the User ?

I have the following two entities, with a corresponding table for each

public class User
{
   private readonly IList<Email> _emails;

   public virtual int Id { get; private set; }
   public virtual IEnumerable<Email> Emails { get { return _emails; } }
   // public virtual Email PrimaryEmail { get; set; } - Possible somehow ?
}
public class Email
{
   public virtual int Id { get; private set; }
   public virtual String Address { get; set; }
   public virtual Boolean IsPrimary { get; set; }
   public virtual User User { get; set; }
}

Can I map a "Email PrimaryEmail" property etc. on the User to the Email which have "IsPrimary=1" set somehow ?
Maybe using a Sql Formula ? a View ? a One-To-One relationship ? or another way ?

It should be possible to change the primary email to be one of the other emails, so i would like to keep them all in 1 table and just change the IsPrimary property.

Using a Sql Formula, is it be possible to keep the "PrimaryEmail" property on the User up-to-date, if I set the IsPrimary property on the current primary email to false, and then afterwards set the PrimaryEmail property to the email which should be the new primary email and set IsPrimary to true ? Will NHibernate track changes on the "old/current" primary Email loaded by the Sql Formula ?
What about the 1 level cache and the 2 level cache when using SqlFormula ?

I dont know if it could work by using a View ? Then i guess the Email could be mapped like a Component ? Will it work when updating the Email data when loaded from the View ?

Is there a better way ?

As I have a bi-directional relationship between User and Email I could in many cases of course query the primary Email and then use the "User" property on the Email to get the User (instead of the other way around - going from User to the primary Email)

Hope someone can help ?

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

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

发布评论

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

评论(2

绝對不後悔。 2024-09-04 09:06:54

乍一看,我想我想确保 PrimaryEMail 始终是对集合中的一个 EMail 对象的引用(一种方法是让获取/设置完全即时进行)。对于像您这样的全公共设计,您必须将更改与电子邮件和/或集合上的事件处理程序挂钩。新的主要电子邮件对象(集合中您要提升为主要的对象)将了解其父集合或用户,因此应该能够找到当前的主要电子邮件对象以使其成为次要电子邮件对象。

替代方案包括使 IsPrimary 不成为公共集,甚至根本不公开它(email.IsPrimary 将替换为 email == user.PrimaryEMail)。 PrimaryEMail set 方法可能只接受集合中已有的 EMail 对象。

我只在手动 ORM 类中完成了此操作,并且找不到在 NHibernate 中执行这种相当常见的场景的规范方法。那里一定有一个。

At first glance, I guess I would want to ensure that the PrimaryEMail is always a reference to one of the EMail objects in the collection (one way of doing that is to have the get/set be completely on-the-fly). With an all-public design like you have, you'll have to hook the change with an event handler on the EMail and/or the collection. The new primary email object (something in the collection which you are promoting to primary) will be aware of its parent collection or user, so should be able to find the current primary to make it secondary.

Alternatives include making the IsPrimary not be a public set, or even not exposing it publicly at all (email.IsPrimary would be replaced with email == user.PrimaryEMail). PrimaryEMail set method might only accept an EMail object already in the collection.

I've only done this in manual ORM classes, and could not find a canonical method for performing this fairly common scenario in NHibernate. There must be one out there.

十年不长 2024-09-04 09:06:54

首先。你为什么在那里使用 IEnumerable ?您想在每次访问该属性时启动查询吗?为什么它有一个公共设置器?

在我看来,您希望它是 IList 或 ICollection。拥有一个列表会更有意义,因为我会添加两种方法:

User.SetPrimary(int index)
User.GetPrimary()
User.ICollection Emails { get; protected set; }

Email.IsPrimary { get; protected set; }

第一个迭代电子邮件以确保只有一个项目设置为主要项目,第二个迭代电子邮件直到找到一个标记为基本的。

First of all. Why are you using an IEnumerable there? Do you want to launch a query each time that property gets accessed? And why does it have a public setter?

Seems to me you want that to be either a IList or ICollection. Having a list would make more sense as I would add two methods:

User.SetPrimary(int index)
User.GetPrimary()
User.ICollection Emails { get; protected set; }

Email.IsPrimary { get; protected set; }

The first iterating through the e-mails to make sure only one item is set to primary and the second that iterates through the e-mails until one is found that is marked as primary.

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