Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:43:07+00:00 2026-05-13T15:43:07+00:00

Assume the following entity classes: public class Player { public virtual int ID {

  • 0

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:

  • A fellow had nearly the same question.
  • Here’s the answer that made the most sense.

  • 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?
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:43:07+00:00Added an answer on May 13, 2026 at 3:43 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume the following schema class Person{ public int Id {get;set;} public virtual ICollection<Province> Provinces
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Assume I have the following two classes: public class User : Entity { public
I have the 2 following entities: @Entity @Table(name = brand) public class Brand {
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
Assume the following type definitions: public interface IFoo<T> : IBar<T> {} public class Foo<T>
I have the following domain class: public class Product { public virtual Guid Id
Consider the following Entity: @Entity public class Employee { @Id @GeneratedValue private Long id;
Assume the following: models.py class Entry(models.Model): title = models.CharField(max_length=50) slug = models.CharField(max_length=50, unique=True) body
assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.