Hibernate- Named Queries
if we want to execute the same queries for multiple times in our program then we can use the Named Queries mechanism
-
In this Named Queries concept, we use some name for the query configuration, and that name will be used whenever the same query is required to execute
-
If you want to create Named Query, in hibernate mapping file we need to configure a query by putting some name for it.
-
For HQL, we need to use
<query name="query_name">
to configure query<query name="bankHQLQuery"> <![CDATA[from BankBo b where b.balance>:bal ]]> </query>
-
For Native SQL, we need to use
<sql-queryname="query_name">
to configure query<sql-query name="bankNativeQuery"> select * from Employee </sql-query>
-
In our main program, we need to use getNamedQuery() given by session interface, for getting the Query reference and we need to execute that query by calling list()
Query qry = session.getNamedQuery("Name given in hib-mapping-xml"); qry.setParameter("bal",new Integer(3000)); List l = qry.list();
Example 1 : HQL Named Query Example
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration SYSTEM
"hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/smlcodes</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="EmployeeBo.hbm.xml" />
<mapping resource="BankBo.hbm.xml" />
</session-factory>
</hibernate-configuration>
BankBo.java
public class BankBo {
private int accno;
private String accname;
private double balance;
//Setters & getters
BankBo.hbm.xml
<hibernate-mapping>
<class name="hql.BankBo" table="bank">
<id name="accno" column="accno" />
<property name="accname" column="accname" />
<property name="balance" column="balance" />
</class>
<query name="bankHQLQuery">
<![CDATA[from hql.BankBo b where b.balance>:bal ]]>
</query>
</hibernate-mapping>
HQLNamedQuery.java
package namedQuery;
public class HQLNamedQuery {
public static void main(String[] args) {
// 1.Load Configuration
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
// 2.Create Session
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
System.out.println("HQL-NamedQuery Example\n----------");
Query qry = session.getNamedQuery("bankHQLQuery");
qry.setParameter("bal", new Double(3000));
List list = qry.list();
Iterator it = list.iterator();
while (it.hasNext()) {
BankBo bo = (BankBo) it.next();
System.out.println(bo.getAccname() + ", " + bo.getBalance());
}
session.close();
sf.close();
}
}
-----------------------------
HQL-NamedQuery Example
-----------------------------
Hibernate: select bankbo0_.accno as accno1_, bankbo0_.accname as accname1_, bankbo0_.balance as balance1_ from bank bankbo0_ where bankbo0_.balance>?
Rakesh, 4000.0
CHANDU, 5000.0
Example 2 : Native SQL Named Query Example
EmployeeBo.java
package bo;
public class EmployeeBo {
private int eid;
private String name;
private String address;
//Setters & getters
}
EmployeeBo.hbm.xml
<hibernate-mapping>
<class name="bo.EmployeeBo" table="employee">
<id name="eid" column="eid">
<generator class="uuid" />
</id>
<property name="name" column="name" />
<property name="address" column="address" />
</class>
<sql-query name="employeeNativeQuery">
select * from Employee
</sql-query>
</hibernate-mapping>
NamedQueryDemo.java
package namedQuery;
public class NamedQueryDemo {
public static void main(String[] args) {
// 1.Load Configuration
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
// 2.Create Session
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Query qry = session.getNamedQuery("employeeNativeQuery");
List list = qry.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Object o[] = (Object[]) it.next();
System.out.println(o[0] + ", " + o[1] + ", " + o[2]);
}
session.close();
sf.close();
}
}
HQL-NamedQuery Example-------------------------
Hibernate: select bankbo0_.accno as accno1_, bankbo0_.accname as accname1_, bankbo0_.balance as balance1_ from bank bankbo0_ where bankbo0_.balance>?
Rakesh, 4000.0
CHANDU, 5000.0