Hibernate Named Query

shape Description

Hibernate Named Query functionality is utilizing queries by indicating name, programmers may utilize either Native SQL, or HQL to perform operations but if programmer might want to use similar queries for multiple times, then Hibernate Named Query mechanism will be used. Here same name will be given to the query configuration that might be used for future purpose. Hibernate Named Query tag will be used in the mapping file. Following are the two ways to create named query.

While using the annotation query as discussed earlier do not need to use mapping file, here @NamedQuery is a single named query and @NamedQueries is multiple named queries.

shape Example

Following is a named query example by using a mapping file where first input should pass through the database.

Employee.java

public class Employee {
	int id;
	String name;
	int salary;
	String job;
	
	
	public String toString(){return id+" "+name+" "+salary+" "+job;}
	
	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 int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	

}

Here created the class Employee by giving variables such as id, name, salary, job. Java bean methods set and get will perform on this variables and This keyword is used to refer the current object.
FetchData.java

import java.util.Iterator;
import java.util.List;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.*;

public class FetchData {
	public static void main(String[] args) {

		AnnotationConfiguration configuration=new AnnotationConfiguration();
		configuration.configure("hibernate.cfg.xml");
		SessionFactory sFactory=configuration.buildSessionFactory();
		Session session=sFactory.openSession();
				
		
		//Hibernate Named Query
		Query query = session.getNamedQuery("findEmployeeByName");
		query.setString("name", "enni");
		
		List employees=query.list();
		
		Iterator itr=employees.iterator();
		while(itr.hasNext()){
		Employee e=itr.next();
		System.out.println(e);
		}
		
		session.close();
	
	}

}

Here hibernate.cfg.xml has configured as follows.

configuration.configure("hibernate.cfg.xml");

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>   
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
<!-- Generated by MyEclipse Hibernate Tools. -->   
<hibernate-configuration>   
 <session-factory>   
 <property name="hbm2ddl.auto">update</property>    
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>    
 <property name="connection.url">jdbc:mysql://localhost:3306/test_db<property>   
 <property name="connection.username">root</property>    
 <property name="connection.password">root</property>  
 <property name="connection.driver_class"<com.mysql.jdbc.Driver>/property>    
     
 <mapping resource="emp.hbm.xml"/>
 </session-factory>  
</hibernate-configuration>

the suitable SQL for the picked database.

Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

emp.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>	 	
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">	 	 
<hibernate-mapping>	 	
<class name="SPlessons.Employee" table="sai">	 	
<id name="id">	 	
<generator class="native"></generator> 	
<property name="name"></property>	 	
<property name="job"></property> 	
<property name="salary"></property>	 	
</class>	 	
<query name="findEmployeeByName">	 	
<![CDATA[from Employee e where e.name = :name]]>	 	
</query>	
 	 	 
</hibernate-mapping> 	

The generator class sub element of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.

Output:

First in the database values should be inserted but name should  be as amit why because in the mentioned that name.

Final output will be as follows.

Summary

shape Key Points

  • While using annotation no need to use mapping file.
  • In the hibernate mapping developer has to mention query element tag.
  • getNamedQuery() is a element was given by session interface.