hibernate使用setResultTransformer()将SQL查询结果放入集合中
@Entity @Table(name="T_BILL_ACCT_ITEM") @NamedQuery(name="TBillAcctItem.findAll", query="SELECT t FROM TBillAcctItem t") public class TBillAcctItem implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="ACCT_ITEM_ID") private long acctItemId; @Column(name="ACCT_ID") private long acctId; @Column(name="BILLING_CYCLE_ID") private String billingCycleId; @Column(name="PRODUCT_ID") private String productId; @Column(name="SERVICE_ID") private String serviceId; @Column(name="ACCT_ITEM_CODE") private String acctItemCode; @Column(name="ORIGINAL_AMOUNT") private int originalAmount; @Column(name="CDR_DISCOUNT") private int cdrDiscount; @Column(name="ACCT_DISCOUNT") private int acctDiscount; @Column(name="RECE_AMOUNT") private int receAmount; @Column(name="REAL_AMOUNT") private int realAmount; @Column(name="CHARGE_OFF_SOURCE") private int chargeOffSource; @Column(name="STATE") private int state;
但是我现在需要查询出来的信息是这样的一个pojo类:(因为int类型的值不能为空,所以我需要将字段定义为 java.long.Integer类型)
public class ProductBillInfo implements Serializable{ private static final long serialVersionUID = 1L; private String productId;//产品标识 private String serverId;//服务标示 private Integer realAmount;//实收金额(sum求和) private Integer receAmount;//应收金额(sum求和) private int state;//账目状态 private String billingCycle;//账期(月)
因为hibernate要查询的对象是要和表结构一一映射的,现在求和字段这些个是映射不了的, 之前的方法就有问题了。 找了很多看了hibernate的API 找到这个东西。 如下:
StringBuffer b = new StringBuffer(); //... 省略SQL拼接代码 String sql = b.toString(); SQLQuery query = session.createSQLQuery(sql); query.addScalar("productId", StandardBasicTypes.STRING);//要查询出来的字段、类型 List<ProductBillInfo> lnfo =query.list();
下面是我用到的查询pojo的方法中的实现:
@SuppressWarnings("unchecked") public List<ProductBillInfo> findByAcctIdAndCycle1(long acctId, String cycleBegin, String cycleEnd, int state){ List<String> cycles = DateUtil.getAllCycle(cycleBegin, cycleEnd); StringBuffer buf = new StringBuffer(); buf.append("select t.PRODUCT_ID as productId , t.SERVICE_ID as serviceId ," + "sum(t.REAL_AMOUNT) as realAmount ,sum(t.RECE_AMOUNT) as receAmount ," + "t.STATE as state ,t.BILLING_CYCLE_ID as billingCycleId " + " from T_BILL_ACCT_ITEM t where t.STATE= \'" + state + "\' AND t.ACCT_ID=\'" + acctId + "\' AND t.BILLING_CYCLE_ID in ("); StringBuffer bf = new StringBuffer(); for(String id : cycles){ if(bf.toString().equals("")){ bf.append("\'"+id+"\'"); } else{ bf.append(","+"\'"+id+"\'"); } } buf.append(bf.toString()); buf.append(") GROUP BY t.PRODUCT_ID,t.BILLING_CYCLE_ID,t.SERVICE_ID"); SQLQuery query = getSession().createSQLQuery(buf.toString()); query.addScalar("productId", StandardBasicTypes.STRING); query.addScalar("serviceId", StandardBasicTypes.STRING); query.addScalar("realAmount", StandardBasicTypes.INTEGER); query.addScalar("receAmount", StandardBasicTypes.INTEGER); query.addScalar("state", StandardBasicTypes.INTEGER); query.addScalar("billingCycleId", StandardBasicTypes.STRING); query.setResultTransformer(Transformers.aliasToBean(ProductBillInfo.class)); List<ProductBillInfo> lnfo =query.list(); return lnfo; }
注:示例中addScalar及setResultTransformer很关键,我在使用的时候还报了一个错(原因是在实体中需要有一个无参构造函数),否则错误如下:
org.hibernate.HibernateException: Could not instantiate resultclass:com.eastelsoft.framework.formbean.interfaceBean.ProductBillInfo
public static void main(String[] args) { List<String> c = new ArrayList<String>(); c.add("1");c.add("2");c.add("3"); StringBuffer bf = new StringBuffer(); for(String id : c){ if(bf.toString().equals("")){ bf.append("\'"+id+"\'"); } else{ bf.append(","+"\'"+id+"\'"); } } System.out.println(bf.toString()); }
打印结果是: \’1\’,\’2\’,\’3\’