|
Named Parameter Map
The following example shows how to use named parameter map.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.tests;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class HQLNamedParamMapTest {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String HQL_QUERY = "from Company comp where comp.id = :company_id and comp.name = :company_name";
Query query = session.createQuery(HQL_QUERY);
Map<String, Comparable> props = new HashMap<String, Comparable>();
props.put("company_id", 4);
props.put("company_name", "IJK");
query.setProperties(props);
List<Company> companies = query.list();
System.out.println("Selected row count : " + companies.size());
for (Company company : companies) {
System.out.println(company);
}
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Selected row count : 1
{ id = 4, name = IJK, employeeCount = 67890,
projectCount = 850, address1 = add1112, address2 = add2223 }
|
|