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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:31:37+00:00 2026-06-05T23:31:37+00:00

Hi i have setup my SessionFactory to cache entities and queries: private ISessionFactory CreateSessionFactory()

  • 0

Hi i have setup my SessionFactory to cache entities and queries:

private ISessionFactory CreateSessionFactory()
{
    var cfg = new Configuration().Proxy(
        properties => properties.ProxyFactoryFactory<DefaultProxyFactoryFactory>()).DataBaseIntegration(
            properties =>
            {
                properties.Driver<SqlClientDriver>();
                properties.ConnectionStringName = this.namedConnection;
                properties.Dialect<MsSql2005Dialect>();
            }).AddAssembly(this.resourceAssembly).Cache(
                properties =>
                {
                    properties.UseQueryCache = true;
                    properties.Provider<SysCacheProvider>();
                    properties.DefaultExpiration = 3600;
                });
    cfg.AddMapping(this.DomainMapping);

    new SchemaUpdate(cfg).Execute(true, true);
    return cfg.BuildSessionFactory();
}

This is my user mapping

public class UserMapping : EntityMapping<Guid, User>
{
    public UserMapping()
    {
        this.Table("USERS");
        this.Property(
            x => x.CorpId,
            mapper => mapper.Column(
                c =>
                {
                    c.Name("CorporateId");
                    c.UniqueKey("UKUserCorporateId");
                    c.NotNullable(true);
                }));
        this.Set(
            x => x.Desks,
            mapper =>
            {
                mapper.Table("DESKS2USERS");
                mapper.Key(km => km.Column("UserId"));
                mapper.Inverse(false);
                mapper.Cascade(Cascade.All | Cascade.DeleteOrphans | Cascade.Remove);
            },
            rel => rel.ManyToMany(mapper => mapper.Column("DeskId")));
        this.Cache(
            mapper =>
            {
                mapper.Usage(CacheUsage.ReadWrite);
                mapper.Include(CacheInclude.All);
            });
    }
}

What I want to do is get a user or query some users and add information to the domain object and cache the updated object.

public class User : Entity<Guid>, IUser
{
    public virtual string CorpId { get; set; }

    public virtual ISet<Desk> Desks { get; set; }

    public virtual MailAddress EmailAddress { get; set; }

    public virtual string Name
    {
        get
        {
            return string.Format(CultureInfo.CurrentCulture, "{0}, {1}", this.SurName, this.GivenName);
        }
    }

    public virtual string GivenName { get; set; }

    public virtual string SurName { get; set; }
}

something like this:

var users = this.session.Query<User>().Cacheable().ToList();

if (users.Any(user => user.EmailAddress == null))
{
    UserEditor.UpdateThroughActiveDirectoryData(users);
}

return this.View(new UserViewModel { Users = users.OrderBy(entity => entity.Name) });

or this:

var user = this.session.Get<User>(id);

if (user.EmailAddress == null)
{
    UserEditor.UpdateThroughActiveDirectoryData(user);
}

return this.View(user);

The UpdateThroughActiveDirectory methods work but are executed everytime i get data from the cache, the updated entities do not keep the additional data. Is there a way to also store this data in nhibernates 2nd level cache?

  • 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-06-05T23:31:39+00:00Added an answer on June 5, 2026 at 11:31 pm

    NHibernate doesn’t cache entire entity in second level cache. It caches only the state / data from the mapped properties. You can read more about it here: http://ayende.com/blog/3112/nhibernate-and-the-second-level-cache-tips

    There’s an interesting discussion in comments of that post that explains this a little further:

    Frans Bouma: Objects need to serializable, are they not? As we’re talking about multiple appdomains. I wonder what’s more
    efficient: relying on the cache of the db server or transporting
    objects back/forth using serialization layers.

    Ayende Rahien: No, they don’t need that. This is because NHibernate doesn’t save the entity in the cache. Doing so would open
    you to race conditions. NHibernate saves the entity data alone,
    which is usually composed of primitive data (that is what the DB can
    store, after all). In general, it is more efficient to hit a cache
    server, because those are very easily scalable to high degrees, and
    there is no I/O involved.

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

Sidebar

Related Questions

I have setup git server via ssh (one git account and many different public/private
I have the following setup in app.config: <hibernate-configuration xmlns=urn:nhibernate-configuration-2.2 > <session-factory name=Nh.Data> <property name=connection.provider>
I have setup a build configuration in TFS2010 with MSBuild arguments to deploy using
I have set up a NInject (using version 1.5) binding like this: Bind<ISessionFactory>().ToMethod<ISessionFactory>(ctx =>
I have setup svnserve and for now now I am testing a repository with
I have setup multiple SQL Service Broker Queues in a database but have not
I have setup coredata in my appDelegate, but it first loads the mainWindow.xib and
I have setup file of winform. but i want to change my icon how
I have setup a git repository in a linux server, and installed the latest
I have setup a network of brokers in activemq, how do i connect to

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.