views:

59

answers:

1

Hi all,

I'd like to run a criteria query with lazy many-to-one associations. Those associations are set as lazy="false" in the HBM. It's because we use it eagerly 90% of the project.

But there are a few 'big' queries that should run as lazy="proxy".

HBM:

<many-to-one name="DestinationElement" class="X" column="DstElemId" not-null="true" unique="false" cascade="save-update" outer-join="auto" fetch="select" lazy="false" index="IDX_Ass_DestElem">

Criteria setup:

criteria.SetFetchMode("DestinationElement", FetchMode.Lazy);

It works the opposite way, but not this way. It fetches eagerly.

The LOC is 20K+, and it'd be a massive refactor to do it the opposite way.

How can I force this to fetch lazily only when I want, and fetch eagerly all other times?

Thanks in advance!

A: 

lazy="false" in the HBM cannot be overriden in a query (besides being a bad idea 99% of the time)

You'll have to change your code.

Diego Mijelshon
Thanks. In the meantime I realized that it couldn't be done. So, we'll change the code.
It'd be good to have this option, but as I knew, it was a design issue why we used some associations eagerly. It was because we needed the eagerly fetched property as a real object: we needed to cast but it was fetched as a parent of the real object so cast was not possible. E.g. We have a Patient that has a property Therapist that is a Person. Actually, this person is always a Doctor that is inherited from Person. So, when I fetch Person lazily, I cannot cast it to Doctor, because only the "Person-part" is available.It was because of having wrong 'Equals' methods
So, getting Therapist lazily as Person, it cannot be cast to Doctor. But, if we set the property to fetch it eagerly, it IS a Doctor. That's why we used it that way. But when fetching multiple Patients, all the Doctors are fetched, too. In this case, it gets real slow. I know it could be joined, too, but that way query size gets 100K+ and Oracle couldn't handle it. So, this was a complex situation where az immediate quick and dirty solution was needed. But as we know, qad solutions are always hit back:D
I've written about how to solve that problem here: http://sessionfactory.blogspot.com/2010/08/hacking-lazy-loaded-inheritance.html
Diego Mijelshon
Very nice solution!!! I'll have to try it out:) I found UnProxy and decided to use that, but I'll give this a try. Hope it will be consistent because I found a very strange behaviour of NH cache.