Hibernate parameter binding

Source: Internet
Author: User

Hibernate parameter binding

There are three methods to bind parameters. In this case, the efficiency is high, that is, two common methods:

Persistence class:

Package entity;

Import javax. persistence. Entity;
Import javax. persistence. GeneratedValue;
Import javax. persistence. GenerationType;
Import javax. persistence. Id;
Import javax. persistence. Table;

// HQl query book information
@ Entity
@ Table
Public class HqlQuery {
Private int id;
Private String name;
Private String author;
@ Id
@ GeneratedValue (strategy = GenerationType. IDENTITY)
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getAuthor (){
Return author;
}
Public void setAuthor (String author ){
This. author = author;
}

Public HqlQuery (){
Super ();
}
Public HqlQuery (String name, String author ){
Super ();

This. name = name;
This. author = author;
}

}

// Query for parameter binding:

Package test;

Import java. util. Iterator;
Import java. util. List;
Import java. util. jar. Attributes. Name;

Import org. hibernate. Query;
Import org. hibernate. Session;
Import org. hibernate. Transaction;

Import entity. HqlQuery;

Import util. HibernateUtil;

// Bind Parameters
Public class ParamTest {

Public static void main (String [] args ){
// ParQue ();
Named ();
}
// Use placeholders
Public static void parQue (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "from HqlQuery where name =? ";
Query query = session. createQuery (hql );
Query. setParameter (0, ""); // set parameters
Iterator it = query. iterate ();
While (it. hasNext ()){
HqlQuery h = (HqlQuery) it. next ();
System. out. println (h. getAuthor ());
}
}
// Use named Parameters
Public static void named (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "from HqlQuery where name =: name ";
Query query = session. createQuery (hql );
Query. setParameter ("name", "Dancing with Wolf ");
Iterator it = query. iterate ();
While (it. hasNext ()){
HqlQuery h = (HqlQuery) it. next ();
System. out. println ("Author:" + h. getAuthor ());
}
}
}

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.