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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:15:09+00:00 2026-05-15T13:15:09+00:00

would you mind help me to better understand what to do with the relationship

  • 0

would you mind help me to better understand what to do with the relationship between my entities and NHibernate?

I have some difficulties to understand what operations I need to do by hand, and what operations NHibernate will do for me (or not).

I have these 2 entities:

public class Position : BaseEntity<int, Position>
{
    private IList<Player> allPlayers = new List<Player>();

    public Position()
    {

    }

    public Position(string name, int number)
        : this()
    {
        Name = name;
        Number = number;
    }

    public string Name { get; set; }
    public int Number { get; set; }
}

and

public class Player : BaseEntity<int, Player>
{
    public Player()
    {
        Visible = true;
    }

    public Player(string firstName, string lastName, int defaultNumber, Sex sex = Sex.Male, Position defaultPosition = null)
        : this()
    {
        FirstName = firstName;
        LastName = lastName;
        DefaultNumber = defaultNumber;
        Sex = sex;
        DefaultPosition = defaultPosition;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Position DefaultPosition { get; set; }
}

Here are the fluent mappings:

public class PositionMap : ClassMap<Position>
{
    public PositionMap()
    {
        Id(pp => pp.Id)
            .GeneratedBy.Increment();

        Map(pp => pp.Name)
            .Not.Nullable();

        Map(pp => pp.Number)
            .Not.Nullable();

        HasMany<Player>(Reveal.Member<Position>("allPlayers"))
            .Access.CamelCaseField();
    }
}

public class PlayerMap : ClassMap<Player>
{
    public PlayerMap()
    {
        Table("Players");

        Id(p => p.Id)
            .GeneratedBy.Increment();

        Map(p => p.FirstName)
            .Not.Nullable()
            .UniqueKey("Players_Unique_FirstName_LastName");

        Map(p => p.LastName)
            .Not.Nullable()
            .UniqueKey("Players_Unique_FirstName_LastName");

        References(p => p.DefaultPosition);
    }
}

As you can see, one player has one position, but may have no position (so DefaultPosition is nullable).

Here are my questions:

  1. When I associate a position to a player’s DefaultPosition, I have to do this through helper methods on Position? (like AddPlayer, DeletePlayer…)

I’d like, when I delete a position, that all the players who have this position, have instead a null DefaultPosition.

  1. Do I have to clear manually the field allPlayers of the position, and manually set null to all associated player’s DefaultPosition, or can NHibernate take care of this for me?

  2. Why does NHibernate only does a DELETE FROM Positions WHERE Id… and doesn’t update the field DefaultPOsition of the concerned players? I’ve tried to add a Cascade.Delete on the HasMany in PositionMap, but that doesn’t change anything. Do I have to run a custom query which does that?

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-15T13:15:10+00:00Added an answer on May 15, 2026 at 1:15 pm

    Q1 If you add a property for Players on Position, then you don’t need those helper methods.

    public class Position : BaseEntity<int, Position>
    {
        private IList<Player> allPlayers = new List<Player>();
    
        //read only
        public IList<Player> Players { get { return allPlayers;} }
    
        //... rest of class omitted
    }
    

    then call like:

    var position = new Position();
    position.Players.Add(new Player());
    

    Q2, Q3 you could have a helper method on position to simplify.

    something like:

    public class Position : BaseEntity<int, Position>
    {
        public void RemoveAll()
        {
           // null out the position on players
           foreach(var player in allPlayers)
           {
               player.Position = null; // SETS PositionId FIELD IN PLAYER TABLE TO NULL
           }
           allPlayers.Clear();
        }
    
        // ... rest of class omitted
    }
    

    with a call looking like:

    using(var session = SessionFactory.GetCurrentSession())
    {
       using(var tx = session.BeginTransaction())
       {
          position.RemoveAll();
          position.Delete();
          tx.Commit();
       }
    }
    

    Since the players will persist beyond the position but the position gets removed, you wouldn’t want to use cascade. Cascade is for deletes, not really updating of id’s. For example, delete an order and cascade all of it’s line items.

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

Sidebar

Related Questions

How would you refactor this, keeping in mind that you have dozens more of
I have a project in mind whose main selling point would be very good
I am working on a test app to help me better understand the way
this is really a newbie question, but it would help me to have a
I'm new to python and I would like some help. I created some classes
Apparently a colon is used in multiple ways in Java. Would anyone mind explaining
Is it possible? From what I have heard it is. I wouldn't mind just
Would the following SQL remove also the index - or does it have to
First of all I am in DESPERATE need of help here PLEASE I will
I know I'm not asking this quite right, either. Please help me better form

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.