Home  >  Hibernate

Example of @NamedQuery in Hibernate Annotation

By Arvind Rai, January 29, 2013
@NamedQuery in hibernate is used to write HQL query. We can write HQL query at entity level. Named query is defined by its name and an associated HQL query. We have a ContactNumber entity at which we have applied named query. In another class we will fetch this named query by getNameQuery of session.
ContactNumber.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name="contact_number")
@NamedQuery(name="contactNumber.specificPerson", query="select c from ContactNumber c where c.personId =:personId")
public class ContactNumber implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	private int id;
	@Column(name="person_id")
	private int personId;
	@Column(name="mobile_number")
	String mobileNumber;
	public ContactNumber(int id,int personId,String mobileNumber){
		this.id=id;
		this.personId=personId;
		this.mobileNumber=mobileNumber;
	}
	public ContactNumber(){	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getPersonId() {
		return personId;
	}
	public void setPersonId(int personId) {
		this.personId = personId;
	}
	public String getMobileNumber() {
		return mobileNumber;
	}
	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}
}
 
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost:3306/hibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="com.concretepage.persistence.ContactNumber"/>

  </session-factory>
</hibernate-configuration>
 
HibernateUtil.java
package com.concretepage.util;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.ContactNumber;
public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
	    static {
	        try {
	            concreteSessionFactory = new AnnotationConfiguration()
	                    .configure().buildSessionFactory();
	        } catch (Throwable ex) {
	            throw new ExceptionInInitializerError(ex);
	        }
	    }
	    public static Session getSession()
	            throws HibernateException {
	        return concreteSessionFactory.openSession();
	    }
	    public static void main(String... args){
	    	Session session=getSession();
	    	session.beginTransaction();
	    	ContactNumber c1=new ContactNumber(1,1,"9891760044");
	    	session.persist(c1);
	    	ContactNumber c2=new ContactNumber(2,1,"9892760044");
	    	session.persist(c2);
	    	ContactNumber c3=new ContactNumber(3,1,"9893760044");
	    	session.persist(c3);
	    	ContactNumber c4=new ContactNumber(4,2,"9894760044");
	    	session.persist(c4);
	       	session.getTransaction().commit();
	       	
	       	Query q = session.getNamedQuery("contactNumber.specificPerson");
	        q.setInteger("personId", new Integer(1));
	        List<?> results = q.list(); 
	        System.out.println("Size of List:"+results.size());
	    }
	} 
Output
Hibernate: insert into contact_number (mobile_number, person_id, id) values (?, ?, ?)
Hibernate: insert into contact_number (mobile_number, person_id, id) values (?, ?, ?)
Hibernate: insert into contact_number (mobile_number, person_id, id) values (?, ?, ?)
Hibernate: insert into contact_number (mobile_number, person_id, id) values (?, ?, ?)
Hibernate: select contactnum0_.id as id0_, contactnum0_.mobile_number as mobile2_0_, contactnum0_.person_id as person3_0_ from contact_number contactnum0_ where contactnum0_.person_id=?
Size of List:3
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us