We can Bind parameters in Hibernate by R4R Team

Methods on Query are provided for binding values to named parameters or JDBC-style ? parameters. Contrary to JDBC, Hibernate numbers parameters from zero. We know that the Named parameters are identifiers of the form: name in the query string. 

The advantages of named parameters are as follows:

1. Named parameters are insensitive to the order they occur in the query string
2. They can occur multiple times in the same query
3. They are self-documenting

//as we can define named parameter (preferred)
Query q = session.createQuery("from DomesticStudent student where student.name = :name");
q.setString("name", "Gourav");
Iterator cats = q.iterate();

//as we can define positional parameter
Query q = session.createQuery("from DomesticStudent student where student.name = ?");
q.setString(0, "Izi");
Iterator cats = q.iterate();

//as we can define named parameter list
List names = new ArrayList();
names.add("Ali");
names.add("Gourav");

Query q = sess.createQuery("from DomesticStudent student where student.name in (:namesList)");
q.setParameterList("namesList", names);
List students = q.list();
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!