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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:25:37+00:00 2026-05-11T13:25:37+00:00

I have a problem with uninitialized proxies in nhibernate The Domain Model Let’s say

  • 0

I have a problem with uninitialized proxies in nhibernate

The Domain Model

Let’s say I have two parallel class hierarchies: Animal, Dog, Cat and AnimalOwner, DogOwner, CatOwner where Dog and Cat both inherit from Animal and DogOwner and CatOwner both inherit from AnimalOwner. AnimalOwner has a reference of type Animal called OwnedAnimal.

Here are the classes in the example:

public abstract class Animal {    // some properties }  public class Dog : Animal {    // some more properties }  public class Cat : Animal {    // some more properties }  public class AnimalOwner  {    public virtual Animal OwnedAnimal {get;set;}    // more properties... }  public class DogOwner : AnimalOwner {    // even more properties }  public class CatOwner : AnimalOwner {    // even more properties } 

The classes have proper nhibernate mapping, all properties are persistent and everything that can be lazy loaded is lazy loaded.

The application business logic only let you to set a Dog in a DogOwner and a Cat in a CatOwner.

The Problem

I have code like this:

public void ProcessDogOwner(DogOwner owner) {    Dog dog = (Dog)owner.OwnedAnimal;    .... } 

This method can be called by many diffrent methods, in most cases the dog is already in memory and everything is ok, but rarely the dog isn’t already in memory – in this case I get an nhibernate ‘uninitialized proxy’ but the cast throws an exception because nhibernate genrates a proxy for Animal and not for Dog.

I understand that this is how nhibernate works, but I need to know the type without loading the object – or, more correctly I need the uninitialized proxy to be a proxy of Cat or Dog and not a proxy of Animal.

Constraints

  • I can’t change the domain model, the model is handed to me by another department, I tried to get them to change the model and failed.
  • The actual model is much more complicated then the example and the classes have many references between them, using eager loading or adding joins to the queries is out of the question for performance reasons.
  • I have full control of the source code, the hbm mapping and the database schema and I can change them any way I want (as long as I don’t change the relationships between the model classes).
  • I have many methods like the one in the example and I don’t want to modify all of them.

Thanks,
Nir

  • 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. 2026-05-11T13:25:38+00:00Added an answer on May 11, 2026 at 1:25 pm

    It’s easiest to turn off lazy loading for the animal class. You say it’s mostly in memory anyway.

    <class name='Animal' lazy='false'> <!-- ... --> </class> 

    As a variant of that, you could also use no-proxy, see this post:

    <property name='OwnedAnimal' lazy='no-proxy'/> 

    As far as I can see, it only works when the AnimalOwner actually is a proxy.

    OR

    You can use generics on the animal owner to make the reference a concrete class.

    class AnimalOwner<TAnimal> {   virtual TAnimal OwnedAnimal {get;set;} }  class CatOwner : AnimalOwner<Cat> { }  class DogOwner : AnimalOwner<Dog> { } 

    OR

    You can map the DogOwners and CatOwners in separate tables, and define the concrete animal type in the mapping.

    <class name='CatOwner'>   <!-- ... -->   <property name='OwnedAninal' class='Cat'/> </class> <class name='DogOwner'>   <!-- ... -->   <property name='OwnedAninal' class='Dog'/> </class> 

    OR

    You mess a little around with NHibernate, as proposed in this blog. NH is actually able to return the real object behind the proxy. Here a bit simpler implementation as proposed there:

        public static T CastEntity<T>(this object entity) where T: class     {         var proxy = entity as INHibernateProxy;         if (proxy != null)         {             return proxy.HibernateLazyInitializer.GetImplementation() as T;         }         else         {             return entity as T;         }     } 

    which can be used like this:

    Dog dog = dogOwner.OwnedAnimal.CastEntity<Dog>(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem with NHibernate. The error message is: Uninitialized proxy passed to
Hello I have a problem of conflict of the namespace. I have a model:
I am dealing with a wierd problem. I have the Config model, together with
I have a problem with hashCode() that delegates to uninitialized objects using hibernate. My
I have a problem with the following code: class SymmetryTypes { public: enum Type
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my

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.