hibernate 中createQuery與createSQLQuery的用法
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;
}
hibernate 中createQuery與createSQLQuery的用法