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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:41:02+00:00 2026-05-20T19:41:02+00:00

I have an Entity with a Collection, which holds other entities having more References

  • 0

I have an Entity with a Collection, which holds other entities having more References on Entities. When the Entity is loaded and later refreshed, the lazy loaded collection is triggered and a lot of stuff is pulled from database which i dont need. So how can i prevent refresh from triggering uninitialized collections?
I have written a small test which shows it.

class SimpleClass
{
    public virtual int Id { get; set; }

    public virtual IList<ChildClass> Childs { get; set; }
}
class ChildClass
{
    public virtual int Id { get; set; }
}

class SimpleClassMap : ClassMap<SimpleClass>
{
    public SimpleClassMap()
    {
        Id(sc => sc.Id).GeneratedBy.Assigned();

        HasMany(sc => sc.Childs)
            .Cascade.All()
            .LazyLoad();
    }
}
class ChildClassMap : ClassMap<ChildClass>
{
    public ChildClassMap()
    {
        Id(cc => cc.Id).GeneratedBy.Assigned();
    }
}

and the following Test

    [Fact]
    public void Test()
    {
        m_session.Save(new SimpleClass
        {
            Id = 1,
            Childs = new List<ChildClass>
            {
                new ChildClass { Id = 1 },
                new ChildClass { Id = 2 },
                new ChildClass { Id = 3 },
            }
        });
        m_session.Flush();
        m_session.Clear();
        log.Debug("Start Test");

        var simple = m_session.Get<SimpleClass>(1);

        var persistentcollection = simple.Childs as IPersistentCollection;
        Assert.False(persistentcollection.WasInitialized, "Before Refresh, collection should not be initialized but was");

        log.Debug("Refresh");
        m_session.Refresh(simple);

        Assert.False(persistentcollection.WasInitialized, "After Refresh, collection should not be initialized but was");
    }

Output:
After Refresh, collection should not be initialized but was

Just to clarify:

Maybe i can live without delete in cascade.all but definitly not without cascade.refresh.
I want refresh to cascade to the collection if it is initialized otherwise not, because it will be lazy loaded with fresh data when used.

I played a little with the NHibernate-code for the Refresh event. If I remove the fetch profile:

DefaultRefreshEventListener {
public virtual void OnRefresh(RefreshEvent @event, IDictionary refreshedAlready)
{
    [...]
    string previousFetchProfile = source.FetchProfile;
    //source.FetchProfile = "refresh";
    object result = persister.Load(id, obj, @event.LockMode, source);
    //source.FetchProfile = previousFetchProfile;
    [...]
}

i get what i expect in the logs (removed aliases in SQL):

without initializing collection

Session.Get
DEBUG - SELECT Id  FROM "SimpleClass" WHERE Id=:p0;:p0 = 1
Refresh
DEBUG - SELECT Id FROM "SimpleClass" WHERE Id=:p0;:p0 = 1

with initializing collection (simple.Childs.Count();)

Session.Get
DEBUG - SELECT Id  FROM "SimpleClass" WHERE Id=:p0;:p0 = 1
simple.Childs.Count();
DEBUG - SELECT SimpleClass_id, Id, Id  FROM "ChildClass" WHERE SimpleClass_id=:p0;:p0 = 1
Refresh
DEBUG - SELECT Id FROM "ChildClass" WHERE Id=:p0;:p0 = 1
DEBUG - SELECT Id FROM "ChildClass" WHERE Id=:p0;:p0 = 2
DEBUG - SELECT Id FROM "ChildClass" WHERE Id=:p0;:p0 = 3
DEBUG - SELECT Id FROM "SimpleClass" WHERE Id=:p0;:p0 = 1

I’m still unsure about other implications of this

  • 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-20T19:41:02+00:00Added an answer on May 20, 2026 at 7:41 pm

    If you look at the generated SQL for the Refresh, you will see that it loads the Children with a left join (and not with a second query, so it is not lazily loaded), while the first Get operation does not have a join.

    Refresh() uses the cascade settings and will load the collections where cascade is set to “all”, so your issue is here:

    HasMany(sc => sc.Childs)
                .Cascade.All()
                .LazyLoad();
    

    If you write Cascade.None or Cascade.SaveUpdate, it should get the desired result. Of course, I don’t know if you absolutely need Cascade.All. In that case I suggest replacing the Refresh() with the following:

    m_session.Evict(simple);
    simple = m_session.Get<Order>(1);
    

    Edit:

    You could examine the initialized-status and do the refresh depending on that. That is probably not the most beautiful solution, though.

    class SimpleClassDAL
    {
        [...]
        public void Refresh(ISession session, SimpleClass simple)
        {
            var persistentcollection = simple.Childs as IPersistentCollection;
            if (persistentcollection.WasInitialized)
            {
                // get everything again
                session.Refresh(simple);
            }
            else
            {
                // only refresh simple
                session.Evict(simple);
                simple = session.Get<SimpleClass>(simple.Id);
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity e.g. Product which aggregates other entities such as Category . Those
I have a Customer entity which references a collection of Addresses. The complication here
im using linq to sql i have a Mission entity, which holds a collection
I have a collection of custom entity objects one property of which is an
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I have got an Entity model which contains a collection of Message objects which
I have entity Ideas, which has child entity collection ChildIdeas. I need to load
I have a situation where I'm adding existing entities to an entity collection. Before
If I have a Blog entity with a BlogEntries collection which may have hundreds
I have an entity which has a collection as one of its attributes. That

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.