我在尝试为以下情况编写查询时遇到了巨大的困难:
在我的核心框架中(我无法修改,因为它不是我们的代码)我有两个类:
[Class(Table = "Teacher")]
public class Teacher
{
[Id]
public virtual long? Id {get;set;}
[Property]
public virtual string EmployeeNr{get;set;}
}
[Class(Table = "Student")]
public class Student
{
[Id]
public virtual long? Id{get;set;}
[Property]
public virtual string Name{get;set;}
}
接下来,在我的应用程序层中,我创建了以下类来记录有关学生和教师进行测试的信息
[Class(Table = "TestReports", DiscriminatorValueObject = 1)]
public class TestReport
{
[Id]
public virtual long? Id{get;set;}
[Property]
public virtual string TestName{get;set;}
[Property]
public virtual DateTime Date{get;set;}
[Property]
public virtual int Correct{get;set;}
[Property]
public virtual int InCorrect{get;set;}
[Property]
public virtual int Unanswerd{get;set;}
}
[Subclass(NameType = typeof(StudentTestReport), ExtendsType = typeof(TestReport), DiscriminatorValueObject = 2)]
public class StudentTestReport
{
[ManyToOne]
public virtual Student Student{get;set;}
}
[Subclass(NameType = typeof(TeacherTestReport), ExtendsType = typeof(TestReport), DiscriminatorValueObject = 3)]
public class TeacherTestReport
{
[ManyToOne]
public virtual Teacher Teacher{get;set;}
}
现在我想要的是一个查询来获取所有学生和老师的最后一个测试报告但是如果老师或学生没有参加任何测试,他们仍然应该出现在概览中。所以我决定采用 AliasToBeanTranformer 方法并为报表创建一个包装器对象:
public class TestOverviewWrapper
{
public virtual string TestName{get;set;}
public virtual DateTime Date{get;set;}
public virtual int Correct{get;set;}
public virtual int InCorrect{get;set;}
public virtual int Unanswerd{get;set;}
public virtual long Id{get;set;}
public virtual string Name{get;set;}
public virtual string EmployeeNr{get;set;}
public virtual string Person
{
get{ return Name ?? EmployeeNr;}
}
}
我在正确查询时遇到了很大的麻烦,我试图看看我是否只能得到学生和他们的最后一个测试报告,但是我卡住了将子查询的属性投影到包装器对象中。这是我走了多远:
public IList<TestOverviewWrapper> GetTestOverview()
{
var crit = SessionFactory.GetCurrentSession().CreateCriteria<Student>("st");
var dcrit = DetachedCriteria.For<StudentTestReport>("lastTest")
.Add(Subqueries.PropertyEq("Date",
DetachedCriteria.For<StudentTestReport>("test")
.Add(Restrictions.EqProperty("lastTest.Student", "st.Id"))
.SetProjection(Projections.Max("lastTest.Date"))));
dcrit.Add(Restrictions.EqProperty("lastTest.Student", "st.Id"));
crit.Add(Subqueries.Select(dcrit));
crit.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("lastTest.Id")
.Add(Projections.Property("lastTest.TestName")
.Add(Projections.Property("lastTest.Date")
.Add(Projections.Property("lastTest.Correct")
.Add(Projections.Property("lastTest.Incorrect")
.Add(Projections.Property("lastTest.Unanswerd")
.Add(Projections.Property("st.Id")
.Add(Projections.Property("st.Name")
);
crit.SetResultTransformer(Transformers.AliasToBean<TestOverview>());
return crit.List<TestOverviewWrapper>();
}
谁能指出我正确的方向,记住我不能在学生和教师班级上添加映射