Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Named Query


The hibernate named query is way to use any query by some meaningful name. It is like using alias names. The Hibernate framework provides the concept of named queries so that application programmer need not to scatter queries to all the java code.

There are two ways to define the named query in hibernate:

  • by annotation
  • by mapping file.

Hibernate Named Query by annotation

If you want to use named query in hibernate, you need to have knowledge of @NamedQueries and @NamedQuery annotations.

@NameQueries annotation is used to define the multiple named queries.

@NameQuery annotation is used to define the single named query.

Let’s see the example of using the named queries:

  1. @NamedQueries(
  2.     {
  3.         @NamedQuery(
  4.         name = “findEmployeeByName”,
  5.         query = “from Employee e where e.name = :name”
  6.         )
  7.     }
  8. )

Example of Hibernate Named Query by annotation

In this example, we are using annotations to defined the named query in the persistent class. There are three files only:

  • Employee.java
  • hibernate.cfg.xml
  • FetchDemo

In this example, we are assuming that there is em table in the database containing 4 columns id, name, job and salary and there are some records in this table.

Employee.java

It is a persistent class that uses annotations to define named query and marks this class as entity.

  1. package com.Kreationnext;
  2. import javax.persistence.*;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6. @NamedQueries(
  7.     {
  8.         @NamedQuery(
  9.         name = “findEmployeeByName”,
  10.         query = “from Employee e where e.name = :name”
  11.         )
  12.     }
  13. )
  14. @Entity
  15. @Table(name=“em”)
  16. public class Employee {
  17.     public String toString(){return id+” “+name+” “+salary+” “+job;}
  18.     int id;
  19.     String name;
  20.     int salary;
  21.     String job;
  22.     @Id
  23.     @GeneratedValue(strategy=GenerationType.AUTO)
  24.     //getters and setters
  25. }

hibernate.cfg.xml

It is a configuration file that stores the informations about database such as driver class, url, username, password and mapping class etc.

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  5. <hibernate-configuration>
  6.     <session-factory>
  7.         <property name=“hbm2ddl.auto”>update</property>
  8.         <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  9.         <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  10.         <property name=“connection.username”>system</property>
  11.         <property name=“connection.password”>oracle</property>
  12.         <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  13.     <mapping class=“com.Kreationnext.Employee”/>
  14.     </session-factory>
  15. </hibernate-configuration>

FetchData.java

It is a java class that uses the named query and prints the informations based on the query. The getNamedQuery method uses the named query and returns the instance of Query.

  1. package com.Kreationnext;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import org.hibernate.cfg.AnnotationConfiguration;
  5. import org.hibernate.*;
  6. public class FetchData {
  7.  public static void main(String[] args) {
  8.   AnnotationConfiguration configuration=new AnnotationConfiguration();
  9.    configuration.configure(“hibernate.cfg.xml”);
  10.    SessionFactory sFactory=configuration.buildSessionFactory();
  11.     Session session=sFactory.openSession();
  12.     //Hibernate Named Query
  13.     Query query = session.getNamedQuery(“findEmployeeByName”);
  14.     query.setString(“name”“amit”);
  15.     List<Employee> employees=query.list();
  16.     Iterator<Employee> itr=employees.iterator();
  17.      while(itr.hasNext()){
  18.     Employee e=itr.next();
  19.     System.out.println(e);
  20.      }
  21.     session.close();
  22.   }
  23. }
download this hibernate example (developed using Myeclipse IDE)

Hibernate Named Query by mapping file

If want to define named query by mapping file, you need to use query element of hibernate-mapping to define the named query.

In such case, you need to create hbm file that defines the named query. Other resources are same as given in the above example except Persistent class Employee.java where you don’t need to use any annotation and hibernate.cfg.xml file where you need to specify mapping resource of the hbm file.

The hbm file should be like this:

emp.hbm.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.           “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  5. <hibernate-mapping>
  6. <class name=“com.Kreationnext.Employee” table=“em”>
  7. <id name=“id”>
  8. <generator class=“native”></generator>
  9. </id>
  10. <property name=“name”></property>
  11. <property name=“job”></property>
  12. <property name=“salary”></property>
  13. </class>
  14. <query name=“findEmployeeByName”>
  15. <![CDATA[from Employee e where e.name = :name]]>
  16. </query>
  17. </hibernate-mapping>

The persistent class should be like this:

Employee.java

  1. package com.Kreationnext;
  2. public class Employee {
  3.     int id;
  4.     String name;
  5.     int salary;
  6.     String job;
  7.     //getters and setters
  8. }

Now include the mapping resource in the hbm file as:

hibernate.cfg.xml

  1. <mapping resource=“emp.hbm.xml”/>

Comments

comments