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 7635703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:29:34+00:00 2026-05-31T07:29:34+00:00

I have two entity classes: Person and Position . public class Person { public

  • 0

I have two entity classes: Person and Position.

public class Person
{
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  public virtual Position Position { get; set; }
}

public class Position
{
  public virtual int Id { get; set; }
  public virtual string Code { get; set; }
}

Person has a one-to-many relationship with Position. I also have a DTO PersonDto that brings the person data from the client.

public class PersonDto
{
  public string Name { get; set; }
  public int PositionId { get; set; }
}

After I get the DTO I have to create a new Person object and persist it. Usually it looks like this:

var person = new Person();
CopyFromDto(person, personDto);
var position = positionRepository.GetById(personDto.PositionId);
person.Position = position;
personRepository.Save(person);

I wonder if I could get rid of this line:

var position = positionRepository.GetById(personDto.PositionId);

The reason is that it produces an unneccessary SQL query that fetches some position data. Is it possible to somehow provide NHibernate with the position id and save a person row with it?

Thanks in advance.

  • 1 1 Answer
  • 0 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-31T07:29:36+00:00Added an answer on May 31, 2026 at 7:29 am

    Use ISession.Load:

    The Load() methods of ISession give you a way to retrieve a persistent
    instance if you already know its identifier.

    Note that Load() will throw an unrecoverable exception if there is no
    matching database row. If the class is mapped with a proxy, Load()
    returns an object that is an uninitialized proxy and does not actually
    hit the database until you invoke a method of the object. This
    behaviour is very useful if you wish to create an association to an
    object without actually loading it from the database.

    https://nhibernate.info/doc/nh/en/index.html#manipulatingdata-loading

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

Sidebar

Related Questions

I have two entity classes: public class Invoice { public int ID { get;
i have the two classes Person and Attribut, in short: @Entity @Indexed @Table(name=persons) public
I have two classes, user and role, defined as: public class User : Entity
I have two classes: News : /** @Entity @Table(name=news) */ class News { /**
For example I have two entities Class A { public Guid Id {get;set;} public
I have two classes: User : /** @Entity @Table(name=users) */ class User { /**
I have two entity classes annotated in the following way @Entity class A {
I'm using play-1.2.3. I have two classes some kind of these: LabelItem: @Entity @Table(name=T_LABEL_ITEM)
I have these two entities. One for Employees: [Table(Name = Employees)] public class Employee
I have two classes. One is the entity class, the other serves as a

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.