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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:15:36+00:00 2026-05-25T22:15:36+00:00

I have the object, which contain the list of objects. public class Product: IProduct

  • 0

I have the object, which contain the list of objects.

public class Product: IProduct
{
    public virtual long     Id   { get; set; }
    public virtual string   Name { get; set; }

    public virtual IList<IFunction> Functions { get; set; }
}

public class Function : IFunction
{
    public virtual long     Id   { get; set; }
    public virtual string   Name { get; set; }

    public virtual IProduct Product { get; set; }
}

Mapped list of objects ( IList < IFunction > ):

<bag name="Functions" cascade="all" table="T_WEBFUNCTIONS" lazy="false">
  <key column="PRODUCT_ID" />
  <one-to-many class="Function" />
</bag>

Code :

I try create a new object of Product

    public IProduct SetProduct(string productName)
    {
         // Product repository, which have nhibernate opened session ( ISession )
         IDBRepository<IProduct> rep = Repository.Get<IProduct>();

         // Create new object of product
         rep.Create(new Product() { Name = productName });

         // get created object by name
         prod = rep.Query().Where(x => x.Name == productName).SingleOrDefault();

         // I HAVE ISSUE HERE
         // prod.Functions == NULL. It must be a new List<Function>()
         // Nhibernate don't create empty list for me and this property always null

         return prod;
    }

BUT. When nhibernate session close and open again, the list will be created and have 0 items.

If I create an object(Product) in the same session and get it after creation, bag will be null.

Function of data repository

public class DBRepository<T> : IDBRepository<T>
{
    protected ISession CurrentSession;

    public DBRepository(ISession session)
    {
        CurrentSession = session;
    }

    public void Create(T obj)
    {
        var tmp = CurrentSession.BeginTransaction();
        CurrentSession.Save(obj);
        tmp.Commit();
    }
}
  • 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-25T22:15:36+00:00Added an answer on May 25, 2026 at 10:15 pm

    When you are calling new Product(), your code is instantiating an object and the List<> property begins life as null. NHibernate can’t help you there – it’s what you’ve decided to do (by not overriding the default c’tor).

    NHibernate’s 1st level cache (short explanation here) saves your object when you commit the transaction, so when in the same open session you ask for the object by its ID NHibernate skips getting the entity from the database and serves you that same instantiated object. You can test that with Object.ReferenceEquals(prodYouCreated, prodRetrievedFromRepository).

    When you close the session the 1st level cache is cleared and next time you query NHibernate fetches the data and builds the object itself – it chooses to give you back a zero-length list instead of null. That is the default behaviour.

    Hope this helps,
    Jonno

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

Sidebar

Related Questions

I have a List of objects which contain a string array as one of
I have an object model like this: public class MyClass { int TheProp {get;set;}
I have an Object X which contains a list of Objects X. I do
I have several TextField columns on my UserProfile object which contain JSON objects. I've
I have an object in my Action class which contains an arraylist of objects
I have this class: public class CampaignMaps : List<CampaignMap> { }; The CampaignMap object
I have a Request object which contains a list of Approvers. An approver has
I have a List which contains a list of objects and I want to
HI all, I created a .net object (ex: A ) which contain data have
I have beans which have Objects which can contain different types. Now when I

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.