Hibernate Named Query Annotations example - Java @ Desk

Friday, November 29, 2013

Hibernate Named Query Annotations example

Named Query can be used in hibernate through Annotation using below classes:
1) import javax.persistence.NamedQueries;
2) import javax.persistence.NamedQuery;

NamedQueries consists of list of NamedQuery which holds each query.

Basic example of NamedQueries consisting of single Named query as shown below
@NamedQueries({ @NamedQuery(name = "getData", query = "select t from CLASS_NAME t") })

CLASS_NAME - Refers to class in which you are using the NamedQuery.

Example of NamedQueries consisting of multiple Named query as shown below
@NamedQueries({
 @NamedQuery(name = "getDataQueryOne", query = "select t from CLASS_NAME t"),
        @NamedQuery(name = "getDataQueryTwo", query = "select t from CLASS_NAME t where CLASS_FIELD_NAME=?") })
CLASS_NAME - Refers to class in which you are using the NamedQuery.
CLASS_FIELD_NAME - Refers to field name inside the class. This should match with the field name where the @Column annotation is used and not the column name from the database.


Complete example of NamedQuery
package com.hibernate.entity;

import java.math.BigInteger;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity
@Table(name = "Employee")
@NamedQueries({ @NamedQuery(name = "getEmployeeById", query = "select t from EmployeeClass t where t.employeeId?") })
public class EmployeeClass {

    @Id
    @Column(name = "empId")
    private BigInteger employeeId;

    @Column(name = "empName")
    private String employeeName;

    // Create getters setters here
    In the named query getEmployeeById, hibernate uses class name in place of table name and java field names instead of column names.
    This is handled using the annotation part.

Question : How does hibernate maps table to classes with the data source. It's shown below using the spring
Hibernate map class with table in datasource. Refer to point 1, 2

How to call the named query created using annotations?
 public List<com.hibernate.entity.Employee> getEmployeeById(BigInteger employeeId) {  
     List<com.hibernate.entity.Employee> employeeList = new ArrayList<com.hibernate.entity.Employee>();  
     try {  
         employeeList= findByNamedQuery(com.hibernate.entity.Employee.class, "getEmployeeById", employeeId);  
         logger.debug("employeeList - " + employeeList);  
     } catch(Exception cause) {  
       cause.printStackTrace();  
     }  
     return employeeList;  
   }  
   public <T> List<T> findByNamedQuery(Class<T> typeClass, String queryName, Object... objects)  
   {  
     Query query = getPopulatedNamedQuery(queryName, objects);  
     List result=query.list();  
     List<T> items = (List<T>) result;  
     return items;  
   }  
   private Query getPopulatedNamedQuery(String queryName, Object... objects) {  
     Query query = getSession().getNamedQuery(queryName);  
     int index = 0;  
     for (Object object : objects) {  
       query.setParameter(index, object);  
       index++;  
     }  
     return query;  
   }  






No comments:

Post a Comment