Followers

hb : HQL NamedQueries Using XML

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

package myhb;

public class Employee {
    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</ployeeroperty>
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="employee.hbm.xml"/>
   
  </session-factory>
</hibernate-configuration>

employee.hbm.xml
============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="myhb.Employee" table="NAMEDQXML">
    <id column="empId" name="empId"/>
    <property column="name" length="20" name="name"/>
      
  </class>
  <query name="searchEmpById"> 
  <![CDATA[from Employee e where e.empId = :id]]> 
  </query>
</hibernate-mapping>

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", 222);           
        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 XML..!!");
          }
        }

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(222);
                emp.setName("Mohan222");
                session.persist(emp);
                tx.commit();
                session.close();
                System.out.println("Success...");
               
        }
}


No comments:

Post a Comment