Two usages of CreateQuery and createsqlquery in hibernate

Source: Internet
Author: User

The difference between CreateQuery and Createsqlquery in Hibernate is:
The former uses the HQL statement to query, the latter can use the SQL statement query
The former takes the bean generated by hibernate as the object to load the list back
The latter is stored as an array of objects

For example, after we get the session:

CreateQuery:

try{
Session session=this.hibernatetemplate.getsessionfactory (). Getcurrentsession ();

Here the table name Dcn_flow is the table name in the database
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 ("Results of DAO layer Execution" +list);

At this point the DAO layer returns an array of type Object

Results of DAO layer execution: [[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++) {

I'm going to traverse the returned results here and put the data into my entity class 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) {

My circuit in this hql is an entity class,:d epartment_id is telling hibernate to deposit parameters in map form
String hql = "from circuit where department_id =:d epartment_id";
map<string, object> params = new hashmap<string, object> ();
Params.put ("department_id", department_id);

A collection of encapsulated circuit objects is returned here
Return Circuitdao.find (HQL, params);
}

The method that I called is encapsulated:

Public list<t> Find (String hql, map<string, object> params) {
Query q = this.getcurrentsession (). CreateQuery (HQL);

to traverse a map
if (params! = null &&!params.isempty ()) {
For (String Key:params.keySet ()) {
Q.setparameter (Key, Params.get (key));
}
}
return Q.list ();
}

Entity class, which corresponds to the entity class and the database field in the form of annotations

@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;
}

Two usages of CreateQuery and createsqlquery in hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.