How to do this using NHibernate's QueryOver?
In my database there are these three tables, among others
TRANSLATION has a non-nullable foreign key to UNIT and a nullable foreign key to ASSIGNMENT. UNIT may have more TRANSLATIONS assigned.
I want to perform a single query which returns a result with the following structure:
Language | Assignment | First SUM | Second SUM | Third SUM
==========================================================
1 | 2 | 456 | 126 | 0
1 | 3 | 5361 | 1367 | 89
... | ... | ... | ... | ...
The results would be grouped by TRANSLATION.fk_language and TRANSLATION.fk_assignment. The main issue for me is to fill the remaining columns:
First SUM = SUM(unit.word_count)
Second SUM = SUM(unit.word_count) WHERE translation.status = 1
Third SUM = SUM(unit.word_count) WHERE translation.status = 2
Even if I restrict myself to the First SUM
column in the result, I can't get it working:
var result = session.QueryOver<Translation>()
.JoinQueryOver<Unit>(x => x.Unit)
.Select(Projections.Group<Translation>(x => x.Language),
Projections.Group<Translation>(x => x.Assignment),
Projections.Sum<Unit>(x => x.WordCount))
.List<object开发者_StackOverflow>()
.ToList();
This fails with
could not resolve property: WordCount of: Entities.Translation
Thank you very much for any suggestions.
EDIT: Here are my mappings:
public class TranslationMap : ClassMap<Translation>
{
public TranslationMap()
{
Table("\"TRANSLATION\"");
LazyLoad();
Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translation", "200");
Map(x => x.Status).Column("status");
References<Language>(x => x.Language, "fk_id_language").Not.Nullable().Cascade.None();
References<Unit>(x => x.Unit, "fk_id_translation_unit").Cascade.None();
References<Assignment>(x => x.Assignment, "fk_id_translator_assignment").Nullable().Cascade.None();
DynamicUpdate();
}
}
public class UnitMap: ClassMap<Unit>
{
public UnitMap()
{
Table("\"UNIT\"");
LazyLoad();
Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translation_unit", "200");
Map(x => x.Text).Column("text");
Map(x => x.WordCount).Column("word_count");
HasMany(x => x.Translations).Inverse().KeyColumn("fk_id_translation_unit").Cascade.None();
}
}
public class AssignmentMap : ClassMap<Assignment>
{
public AssignmentMap()
{
Table("\"TRANSLATOR_ASSIGNMENT\"");
LazyLoad();
Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translator_assignment", "50");
}
}
Did you map the WordCount of property of your Unit entity? (do you have a hbm.xml like this)
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
<class name="Unit">
<id name="Id" column="Id" type="Int32" >
<generator class="native" />
</id>
<property name="WordCount" colum="word_count"/>
</class>
</hibernate-mapping>
精彩评论