Followers

hb : HQL NamedQueries Using Annotation

Employee.java
==========

package myhb;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@NamedQueries( 
    { 
        @NamedQuery( 
        name = "searchEmpById", 
        query = "from Employee e where e.empId = :id" 
        ) 
    } 

@Entity
@Table(name = "NAMEDQA")
public class Employee implements Serializable{
    @Id
    private int empId;
    private String name;

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


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">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/myhb</property>
    <property name="hibernate.connection.username">app</property>
    <property name="hibernate.connection.password">app</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping class="myhb.Employee" ></mapping>
   
  </session-factory>
</hibernate-configuration>

OurLogic.java
=========

package myhb;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class OurLogic {

          public static void main(String args[])
          {
        Configuration cfg = new Configuration();
          cfg.configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();
          Session session = factory.openSession();
          Transaction tx = session.beginTransaction();
        //Hibernate Named Query 
        Query query = session.getNamedQuery("searchEmpById");
        query.setInteger("id", 999);           
        List<Employee> employees=query.list();
        Iterator<Employee> itr=employees.iterator(); 
        while(itr.hasNext()){ 
        Employee e=itr.next();
        System.out.println(e.getEmpId()+" "+e.getName()); 
            } 
          tx.commit();
        session.close();
          System.out.println("Named Queries using Annotation completed..!!");   
          }
}

SaveData.java
=========
package myhb;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SaveData {   
          public static void main(String args[])
          {                 
          Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
          SessionFactory factory = cfg.buildSessionFactory();
          Session session = factory.openSession();
          Transaction tx = session.beginTransaction();
        Employee emp = new Employee();
        emp.setEmpId(999);
        emp.setName("Mohan222");
        session.persist(emp);
        tx.commit();
        session.close();
        System.out.println("Success...");               
        }   

}

No comments:

Post a Comment