Skip to main content

NamedQuery In Hiibernate

What is Use of Named Query

In large enterprise application has lots of code.

Suppose in different program you have written different query
then maintenance become to difficult.

Named Query used put your all query in single code.
And you can use that query in different program by using query name.

How to define named query

1
2
 
@NamedQuery(name="Userdata.byid", query=" from Userdata where id > ? ")

How to refer that query in different program

1
2
 
Query query=session.getNamedQuery("Userdata.byid");

What is advantage of named query.

  • Easy to organize
  • Your all query placed in single code so maintenance is easy.

Let’s see complete Example.

  • Userdata.java
  • Hibernate.cgf.xml
  • Namedquery.java

Userdata.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
package com.spy.operation;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persitance.NamedQuery;
 
 
@Entity
@NamedQuery (name="Userdata.byid", query=" from Userdata where id > ? ")
public class Userdata
{
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int id;
	String user;
 
	public int getId() 
	{
	   return id;
	}
	public void setId(int id) 
	{
	   this.id = id;
	}
	public String getUser()
	{
	  return user;
	}
	public void setUser(String user)
	 {
	 this.user = user;
	}
}

Hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 
<session-factory>
 
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
 
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
 
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
 
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
 
<mapping class="com.spy.operation.Userdata"/>
 
</session-factory>
</hibernate-configuration>

Namedquery.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
package com.spy.operation;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Namedquery
{
 
	public static void main(String[] args) 
	{
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
		Session session=sf.openSession();
 
		session.beginTransaction();
 
		String userid="5";
		Query query=session.getNamedQuery("Userdata.byid");
 
		query.setInteger(0,Integer.parseInt(userid));
 
		List<Userdata> users=query.list();
 
		session.getTransaction().commit();
 
		session.close();
 
		for (Userdata userdata : users)
		{
		   System.out.println(userdata.getUser());
		}
 
		System.out.println("****************");
	}
}

Output:

1
2
3
4
5
6
7
8
 
Hibernate: select userdata0_.id as id1_, userdata0_.user as user1_ from
Userdata userdata0_ where userdata0_.id>?
user 6
user 7
user 8
user 9
****************