views:

37

answers:

1

Hello all, consider these POCOs:

class Foo {
  int Id {get;set;}
  string Name {get;set;}
}

class Bar {
  int Id {get;set;}
  string PropA {get;set;}
  Foo PropB {get;set;} 
}

Now what i want to achieve is using an ISQLQuery with a root entity of Bar to also hydrate the PropB property.

ISQLQuery barsAround = nhSes.CreateSQLQuery("select b.Id, b.PropA, {????} from Bar b inner join Foo f on f.Id = b.FK_FooId");
barsAround.SetResultTransformer(Transformers.AliasToBean<Bar>());
IList<Bar> results = barsAround.List<Bar>();

where in the {????} is the fragment that fetches the b.Id and b.Name and hydrates the property PropB of entity Bar.

I cannot use ISQLQuery.AddEntity() because that results in managed entities and i cannot use managed entities. The bars fetched are versions of a bar so the same Id for each row kills the NHibernate engine.

+1  A: 

You can to write your own ad-hoc transformer. It's not hard, look at the source of AliasToBeanResultTransformer.

A better alternative is writing an object model that actually reflects your DB and domain. If there are multiple versions of Bar, then the version number (or whatever is used to identify it) should be part of the key.

That way you'd get rid of all the hacks.

Diego Mijelshon
no, normal application operation always gets the latest version. Its for specific parts (like auditing) that i want a list of the versions of a specific entity with a given id.... i will look into a custom transformer, do you have any examples i can look into?
Jaguar