使用方法举例如下:
public List findByOid(Object oid) {
log.debug("finding all WatershedAnalyse instances");
Session session=getSessionFactory().openSession();
SQLQuery query=null;
try {
String queryString = "SELECT oid,bdatetime,basin_code,p,e FROM basin_in WHERE oid="+oid;
query=session.createSQLQuery(queryString);
query.addScalar("oid", Hibernate.INTEGER);
query.addScalar("bdatetime", Hibernate.STRING);
query.addScalar("basin_code", Hibernate.STRING);
query.addScalar("p", Hibernate.DOUBLE);
query.addScalar("e", Hibernate.DOUBLE);
query.setResultTransformer(Transformers.aliasToBean(BasinIn.class));
return query.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}finally{
session.close();
}
}
使用setResultTransformer 要使用query.addScalar("oid", Hibernate.INTEGER)指定
返回类型 否则会抛出以下异常。
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.serialssoloutions.database.entity.ProductInfo.id
Caused by: java.lang.IllegalArgumentException: argument type mismatch
BasinIn bean如下
public class BasinIn implements java.io.Serializable {
private Integer oid;
private String bdatetime;
private String basin_code;
private Double p;
private Double e;
public BasinIn() {
}
public String getBasin_code() {
return basin_code;
}
public void setBasin_code(String basin_code) {
this.basin_code = basin_code;
}
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public Double getP() {
return p;
}
public void setP(Double p) {
this.p = p;
}
public Double getE() {
return e;
}
public void setE(Double e) {
this.e = e;
}
public String getBdatetime() {
return bdatetime;
}
public void setBdatetime(String bdatetime) {
this.bdatetime = bdatetime;
}
}