NHibernate:多对一 - *必须*加载父对象吗?

发布于 2024-08-19 16:40:29 字数 2680 浏览 7 评论 0原文

假设以下实体类:

public class Player
{
 public virtual int ID { get; set; }
 public virtual string Name { get; set; }
 public virtual Team Team { get; set; }
}

public class Team
{
 public virtual int ID { get; set; }
 public virtual string City { get; set; }
 public virtual string Nickname { get; set; }
}

假设以下 Player 映射类:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
 <class name="Player">
  <id name="ID" column="ID" type="System.Int32" unsaved-value="null">
   <generator class="native"/>
  </id>
  <property name="Name" column="Name" not-null="true" type="System.String"  length="50" insert="true" update="true"/>
  <many-to-one name="Team" not-null="true" outer-join="auto" insert="true" update="true">
   <column name="TeamID"/>
  </many-to-one>
 </class>
</hibernate-mapping>

并假设以下 Player 存储库方法:

public void Add(Player player)
{
 using (ISession session = NHibernateHelper.OpenSession())
 {
  using (ITransaction transaction = session.BeginTransaction())
  {
   session.Save(player);
   transaction.Commit();
  }
 }
}

我的问题:

当我想要创建一个团队时,我必须加载一个成熟的团队(父对象)吗?新玩家?
或者我可以指定一个“模拟”对象,并且只指定外键吗?

Player player = new Player
              {
               Name = "Tom Brady",
               Team = new TeamRepository().GetTeamByCityAndNickname("New England", "Patriots") // Is this the only way?
               // or can I do this?
               // Team = new Team { ID = 22 }
              };
new PlayerRepository().Add(player);

  • 如果我不能指定“模拟” 对象(仅指定 外键),你能解释一下吗 为什么我不能?
  • 也就是说,您能告诉我幕后发生的事情吗?

注意:


  • 有趣的是,当谈到 EF 时 4.0 DotNetRocks 剧集期间,Julia Lerman 承认很多人想要 在这些类型中使用外键 情况。

编辑: 这个答案< /a> 指出了我问题的本质。

把它想象成有一个对象 只保留 Id 并且将加载 剩下的,如果你需要的话。如果 你只是把它传递给 建立关系(如 FK), id 就是您所需要的。

  • 好吧,如果是这样的话,那么为什么我需要担心代理对象之类的事情呢?如果这才是真正重要的,为什么我不能只创建一个“虚拟”对象并指定外键值?

Assume the following entity classes:

public class Player
{
 public virtual int ID { get; set; }
 public virtual string Name { get; set; }
 public virtual Team Team { get; set; }
}

public class Team
{
 public virtual int ID { get; set; }
 public virtual string City { get; set; }
 public virtual string Nickname { get; set; }
}

Assume the following mapping class for Player:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
 <class name="Player">
  <id name="ID" column="ID" type="System.Int32" unsaved-value="null">
   <generator class="native"/>
  </id>
  <property name="Name" column="Name" not-null="true" type="System.String"  length="50" insert="true" update="true"/>
  <many-to-one name="Team" not-null="true" outer-join="auto" insert="true" update="true">
   <column name="TeamID"/>
  </many-to-one>
 </class>
</hibernate-mapping>

And assume the following Player repository method:

public void Add(Player player)
{
 using (ISession session = NHibernateHelper.OpenSession())
 {
  using (ITransaction transaction = session.BeginTransaction())
  {
   session.Save(player);
   transaction.Commit();
  }
 }
}

My question:

Must I load a full-fledged Team (parent object) when I want to create a new Player?
Or can I specify a "mock" object, and only specify the foreign key?

Player player = new Player
              {
               Name = "Tom Brady",
               Team = new TeamRepository().GetTeamByCityAndNickname("New England", "Patriots") // Is this the only way?
               // or can I do this?
               // Team = new Team { ID = 22 }
              };
new PlayerRepository().Add(player);

  • And if I can't specify a "mock"
    object (specifying only the
    foreign key), can you please explain
    why I can't?
  • That is, can you please give me an idea about what's going on under the hood?

Heads-up:


  • Interestingly, when speaking about EF
    4.0 during a DotNetRocks episode, Julia Lerman
    acknowledged that many people want to
    use the foreign key in these types of
    situations.

EDIT: This answer points to the essence of my question.

Think of it like having an object that
only keeps the Id and that will load
the rest if you ever need it. If
you're just passing it arround to
create relationships (like FKs), the
id is all you'll ever need.

  • Well if that's the case, then why do I need to be worried about proxy objects and such? Why can't I just create a "dummy" object and specify the foreign key value if that's all that really matters?

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

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

发布评论

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

评论(2

美胚控场 2024-08-26 16:40:29

你像这样使用外键...

Team = session.Load<Team>(id);

知道 load 和获取

you use the foreign key like so...

Team = session.Load<Team>(id);

know the difference between load and get

奈何桥上唱咆哮 2024-08-26 16:40:29

如果此时你可以访问 Session ,你可以调用

Team = Session.Load<Team>(id);

Load 的前提是它会创建一个 NHibernate 代理,如果需要的话可以自行解析。当然,您必须确保 id 存在,否则如果它尝试自行解析,您将收到 EntityNotFound 错误。

If you have access to Session at this point you can call

Team = Session.Load<Team>(id);

The premise of Load is that it will create an NHibernate proxy that can resolve itself if needed. Of course you have to be sure the id exists or you will get an EntityNotFound error if it ever tries to resolve itself.

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