我正在使用Hibernate(也是新的)并尝试使用聚合函数从MS SQL数据库中检索计数值和其他字段。我为数据创建了一个POJO类,如下所示:
package com.hdl.model.db;
import java.util.Date;
@Entity
@Table(name = "sfdc_stg_lab_orders")
@SqlResultSetMappings( {
SqlResultSetMapping(name = "ProfessorAndManager",
columns = { @ColumnResult(name = "total"),
@ColumnResult(name = "org_name"),
@ColumnResult(name = "drawMonth"),
@ColumnResult(name = "drawYear")
})
})
public class OrgnameByMonthYear {
public OrgnameByMonthYear(Id sfdc_stg_lab_order_key, String org_name,int drawMonth,
int drawYear , Double total){
this.org_name = org_name;
this.total = total;
this.drawMonth = drawMonth;
this.drawYear = drawYear;
}
@Id
@GeneratedValue
@Column(name= "sfdc_stg_lab_order_key")
/*
* Unique ID - System Generated
*/
private Integer sfdc_stg_lab_order_key;
/*
* Name of the Organization
*/
@Column(name= "org_name")
private String org_name;
@Column(name = "total")
private double total;
@Column(name = "drawMonth")
private int drawMonth;
@Column(name = "drawYear")
private int drawYear;
public Integer getSfdc_stg_lab_order_key() {
return sfdc_stg_lab_order_key;
}
public void setSfdc_stg_lab_order_key(Integer sfdc_stg_lab_order_key) {
this.sfdc_stg_lab_order_key = sfdc_stg_lab_order_key;
}
/**
* @return the orgname
*/
public String getOrg_name() {
return org_name;
}
/**
* @param orgname to set
*/
public void setOrg_name(String org_name) {
this.org_name = org_name;
}
/**
* @return the year
*/
public double getTotal() {
return total;
}
/**
* @param total to set
*/
public void setTotal(long total) {
this.total = total;
}
/**
* @return the month
*/
public int getDrawMonth() {
return drawMonth;
}
/**
* @param month to set
*/
public void setDrawMonth(int drawMonth) {
this.drawMonth = drawMonth;
}
/**
* @return the year
*/
public int getDrawYear() {
return drawYear;
}
/**
* @param year to set
*/
public void setDrawYear(int drawYear) {
this.drawYear = drawYear;
}
@Override
public String toString() {
return "sfdc_stg_lab_orders [sfdc_stg_lab_order_key=" +
sfdc_stg_lab_order_key + "total=" + total + ", org_name=" + org_name + "]";
}
}
我正在使用Hibernate find调用以下内容来检索数据:
@SuppressWarnings("unchecked")
@Override
public List<OrgnameByMonthYear> getOrgnameByMonthYear() {
logger.info("Retrieving getOrgnameByMonthYear list inside SfdcStgLabOrdersDAOImpl ....");
return hibernateTemplate.find("select count(org_name) AS total, org_name,
month(specimen_draw_date_1) AS drawMonth, year(specimen_draw_date_1) AS drawYear from
OrgnameByMonthYear group by org_name, month(specimen_draw_date_1),
year(specimen_draw_date_1)");
}
我在Java中遇到以下错误“无法转换为OrgnameByMonthYear类”。提前感谢您的任何帮助!
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'executeFirstTask' on target class [class com.hdl.service.impl.SchedulerService] failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.hdl.model.db.OrgnameByMonthYear at
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:320) at
org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:223)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.hdl.model.db.OrgnameByMonthYear
答案 0 :(得分:0)
这可以通过创建一个包含查询结果的新类AggregationResults来完成:
public class AggregationResults {
private Integer total;
private String orgName;
private Integer drawMonth;
private Integer drawYear;
... constructor with all properties here ...
}
然后重写查询,以便使用AggregationResults
运算符返回new
:
@SuppressWarnings("unchecked")
@Override
public List<AggregationResults> getOrgnameByMonthYear() {
logger.info("Retrieving AggregationResults list inside SfdcStgLabOrdersDAOImpl ....");
return hibernateTemplate.find("select new com.your.package.AggregationResults( count(org_name) AS total, org_name,
month(specimen_draw_date_1) AS drawMonth, year(specimen_draw_date_1) AS drawYear) from
OrgnameByMonthYear group by org_name, month(specimen_draw_date_1),
year(specimen_draw_date_1)");
}