hibernate 中createQuery与createSQLQuery两者区别是:
前者用的hql语句进行查询,后者可以用sql语句查询
前者以hibernate生成的Bean为对象装入list返回
后者则是以对象数组进行存储

比如我们在获得session后:

createQuery:

try{
  Session session=this.hibernateTemplate.getSessionFactory().getCurrentSession();

//这里的表名dcn_flow是数据库里的表名
  String sql="select local,remote,object_id from dcn_flow where is_NPI <> 9 ";
  Query query=session.createSQLQuery(sql);
  List list = query.list();

  System.out.println("dao层执行的结果"+list);

//此时dao层返回的是一个object类型的数组

dao层执行的结果:[[Ljava.lang.Object;@da341, [Ljava.lang.Object;@1592141, [Ljava.lang.Object;@9652e4, [Ljava.lang.Object;@11c3288, [Ljava.lang.Object;@399c02, [Ljava.lang.Object;@317dc9, [Ljava.lang.Object;@1394e9d, [Ljava.lang.Object;@1583882, [Ljava.lang.Object;@e60a94, [Ljava.lang.Object;@c0843d, [Ljava.lang.Object;@129dff4, [Ljava.lang.Object;@2e0dcb, [Ljava.lang.Object;@18ba429]


  List<DCN_FlowVo> dcnList=null;
  if(list!=null&&list.size()>0){
    dcnList=new ArrayList<DCN_FlowVo>();
    for(int i=0;i<list.size();i++){

//我在此处遍历返回的结果将数据放到我的实体类DCN_FlowVo里

    Object[] obj=(Object[])list.get(i);
    DCN_FlowVo dcn=new DCN_FlowVo();
    if(obj[0]!=null){
      dcn.setLocal(obj[0].toString());
    }
    if(obj[1]!=null){
      dcn.setRemote(obj[1].toString());
    }
    if(obj[2]!=null){
      dcn.setObject_id(obj[2].toString());
    }
    dcnList.add(dcn);
  }
}

return dcnList;
}catch(Exception e){
System.out.println(e);
return null;
}

 

createSQLQuery:

public List<CirCuit> findByDepartment_id(String department_id){

//我这个hql里的CirCuit是实体类 ,:department_id 是告诉hibernate以map形式存入参数
  String hql = " FROM CirCuit where DEPARTMENT_ID = :department_id ";
  Map<String, Object> params = new HashMap<String, Object>();
  params.put("department_id", department_id);

//此处返回的是封装circuit对象集合
  return cirCuitDao.find(hql, params);
}

调用的方法是我封装好的:

public List<T> find(String hql, Map<String, Object> params) {
  Query q = this.getCurrentSession().createQuery(hql);

//对map进行遍历
    if (params != null && !params.isEmpty()) {
    for (String key : params.keySet()) {
      q.setParameter(key, params.get(key));
    }
  }
  return q.list();
}

//实体类,通过注解形式把实体类和数据库字段对应上

@Entity
@Table(name = "CMDB.NE_CIRCUIT_RESOURCE")
@SequenceGenerator(name="objectId",allocationSize=1,initialValue=1,sequenceName="cmdb.SEQ_circuit_objectID")
public class CirCuit implements Serializable{

private static final Long SERIALVERSIONUID = -3898355999058501880L;

private Long objectId;
private Double gt_version;
private String circuit_no;
private String department_id;

@Id

@Column(name="OBJECTID",nullable=false)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="objectId")
public Long getObjectId() {
return objectId;
}

public void setObjectId(Long objectId) {
this.objectId = objectId;
}
@Column(name="GT_VERSION")
public Double getGt_version() {
return gt_version;
}

public void setGt_version(Double gt_version) {
this.gt_version = gt_version;
}
@Column(name="CIRCUIT_NO",length=150,nullable=false)
public String getCircuit_no() {
return circuit_no;
}

public void setCircuit_no(String circuit_no) {
this.circuit_no = circuit_no;
}
@Column(name="DEPARTMENT_ID",length=173)
public String getDepartment_id() {
return department_id;
}

public void setDepartment_id(String department_id) {
this.department_id = department_id;
}

 

posted on 2015-12-29 18:16  田梦伟  阅读(23318)  评论(1编辑  收藏  举报