NativeNamedQuery in Hibernate

In Namedquery we write HQL query.
But Now if you want Use SQL query then we use NativeNamedQuery.

Synatx for NativeNamedQuery query

1
2
3
4
 
@NamedNativeQuery(name = "Userdata.byname",
                  query = "Select *from Userdata where id=? and user=?",
                  resultClass=Userdata.class )

It has Three Attribute:

Name: named your according to its fuctionality
query: you can define Sql query
resultclass: This is your Java Bean class detail

How to Refer Query:

1
  Query query2=session.getNamedQuery("Userdata.byname");

Let’s see complete Example.

  • Hibernate.cgf.xml
  • Userdata.java
  • Nativenamedquery.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
 
package com.spy.operation;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedNativeQuery;
 
@Entity
@NamedNativeQuery(name = "Userdata.byname", query = "Select *from Userdata where id=? and
user=?",resultClass=Userdata.class )
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>

Nativenamedquery.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 sessionfactory=new Configuration().configure().buildSessionFactory();
 
		Session session=sessionfactory.openSession();
 
		session.beginTransaction();
 
		Query query2=session.getNamedQuery("Userdata.byname");
 
		query2.setInteger(0,1);
 
		query2.setString(1,"user 1");
 
		List<Userdata> u1= query2.list();
 
		session.getTransaction().commit();
 
		session.close();
 
		for(Userdata u:u1)
		{
		  System.out.println(u.getUser());
		}
	}
}

Output:

1
2
3
 
Hibernate: Select *from Userdata where id=? and user=?
user 1

Leave a Reply

Your email address will not be published. Required fields are marked *