嗨我喜欢月亮表jan,feb..dec,我在每个表中都有locatin和actioncount字段,每个表都有重复的位置。
我的查询大致是用SQL编写的,我有月域对象,现在我必须将它转换为Hibernate查询(HQL或Criteria api或其他任何东西......)。我该如何转换它?
月份的数量是作为列表提供的,并且是可变的
下面的sql来自此列表monthsToQuery = [oct,nov,dec,jan]
..这也是变量列表。
它可以是[feb,mar,apr]或[jul]
select loc, sum(tcount) from (
(select location as loc, sum(actioncount) as tcount from oct group by location) left-join
(select location as loc, sum(actioncount) as tcount from nov group by location) left-join
(select location as loc, sum(actioncount) as tcount from dec group by location) left-join
(select location as loc, sum(actioncount) as tcount from jan group by location)
) group by loc
我正在做左联,因为我不想在不同月份之间放松任何地点。
另外:我还有一个日期范围作为输入。到目前为止,我正在从该范围获得一个月的列表,并获得每个月separatley的结果。我需要编写查询以在1个查询中提供最终所需的结果。 这是我到现在所拥有的:
// sTblList - list of all month domains in the date range..
def getSummary(sTblList,SfromDate,StoDate,res_id, groupCol,sumCol){
try{
Date fromDate = new Date().parse("yyyy-MM-dd", SfromDate);
Date toDate = new Date().parse("yyyy-MM-dd", StoDate);
def resourceInstance=Resources.get(res_id);
sTblList.each{
def OnemonthList=it.createCriteria().get {
eq('graresource',resourceInstance)
between('currentdate', fromDate, toDate)
projections {
sum(sumCol,'tcount')
groupProperty(groupCol)
}
}
return sumMap // sumMap should have all months results combined
}
我读了一些地方而不是嵌套标准我也可以在标准中使用别名。我是新来的.. 还有人知道吗?
答案 0 :(得分:1)
如果您的模型使用继承
abstract class Month
{
string location;
int actionCount;
}
class January extends Month
{
}
session.CreateCriteria(Month.class)
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Location"))
.Add(Projections.Sum("ActionCount")))
或界面
class Month implements HasActionCount
{
string location;
int actionCount;
}
session.CreateCriteria(HasActionCount.class)
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Location"))
.Add(Projections.Sum("ActionCount")))
更新:以下适用于NHibernate和SQLite(也应该在Hibernate中使用)
class Month
{
public virtual int Id { get; set; }
public virtual string Location { get; set; }
public virtual int ActionCount { get; set; }
}
class January : Month
{
}
class February : Month
{
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.January, ConsoleApplication1" table="`January`">
<id name="Id">
<generator class="identity" />
</id>
<property name="Location" />
<property name="ActionCount" />
</class>
<class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.February, ConsoleApplication1" table="`February`">
<id name="Id">
<generator class="identity" />
</id>
<property name="Location" />
<property name="ActionCount" />
</class>
</hibernate-mapping>
// works as expected
IList<Month> months = session.CreateCriteria<Month>().List<Month>();
// returns the location and sum of each month though
IList<Object[]> sums = (Object[])session.CreateCriteria<Month>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Location"))
.Add(Projections.Sum("ActionCount")))
.List();