I have a question about formulas. Here is some context: part of my model looks like this:
Trial (1) --> (n) ExperimentalUnit (1) --> (n) Observation
It is an important state of a trial whether is has observations or not, i.e. is there any ExperimentalUnit with any Observation? I hate redundant state fields, so I want to determine this at runtime. However, when loading a list of trials it is a complete overkill to run through their ExperimentalUnit collections and Observation collections of each ExperimentalUnit until one is found with a count > 0. So I wanted to give my Trial a property with a formula like:
Code:
<property
name="HasAnyObservation"
formula="( select 1 from Trial trl where trl.TrlId = TrlId and (exists (select 1 from ExperimentalUnit eu where eu.TrlId = TrlId and (exists (select 1 from Observation obs where obs.ExpId = eu.ExpId)))) )"
type="String"
/>
It appears that I am stretching NHibernate beyond its limits here. The generated query is:
SELECT trials0_.TrlId as TrlId__, ... , ( select 1 from Trial trl where trl.TrlId = trials0_.TrlId and
trials0_.exists (select 1 from ExperimentalUnit eu where eu.TrlId = trials0_.TrlId and
trials0_.exists (select 1 from Observation obs where obs.ExpId = eu.ExpId)) ) as f2_0_ FROM Trial trials0_ WHERE trials0_.CrpId=@p0
As you see, the 'exists' is seen as a field name and the query is nonsense.
I discovered that someone had found a similar issue before
, here, and proposed a solution. Is there any chance that the potentially powerful formula feature will be 'fixed' to accomodate this kind of usage?