Followers

hb : HCQL(Projections)

HCQL (Hibernate Criteria Query Language)

public Criteria setProjection(Projection projection) specifies the projection.
       ProjectionList p1=Projections.projectionList();
       p1.add(Projections.property("name"));
       p1.add(Projections.property("salary"));       
       cr.setProjection(p1);


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

package mypack;
import javax.persistence.*;
@Entity
@Table(name = "MYEMP")
public class Employee {
@Id
private int id;
private String name;
private float salary;
private String address;
public Employee() {
          super();
}

public Employee(String name, float salary,String address) {
          super();
          this.name = name;
          this.salary = salary;
        this.address = address;
}

public int getId() {
          return id;
}
public void setId(int id) {
          this.id = id;
}
public String getName() {
          return name;
}
public void setName(String name) {
          this.name = name;
}
public float getSalary() {
          return salary;
}
public void setSalary(float salary) {
          this.salary = salary;
}

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

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

</hibernate-configuration>

SingleProjections.java
==============

package mypack;
import java.util.Iterator;
import java.util.List;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.Projections;

public class SingleProjection {
public static void main(String[] args) {
       SessionFactory factory =new AnnotationConfiguration().configure().buildSessionFactory();
       Session session=factory.openSession();
       Criteria cr = session.createCriteria(Employee.class);
       cr.setProjection(Projections.property("name"));
       List list = cr.list();
       Iterator itr = list.iterator();
       while(itr.hasNext()){
       String name = (String)itr.next();
       System.out.println(name);
       }      
        session.close();
    }       
}

MultipleProjections.java
===============

package mypack;
import java.util.Iterator;
import java.util.List;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

public class MultipleProjections {
public static void main(String[] args) {
       SessionFactory factory =new AnnotationConfiguration().configure().buildSessionFactory();
       Session session=factory.openSession();
       Criteria cr = session.createCriteria(Employee.class);
       ProjectionList p1=Projections.projectionList();
       p1.add(Projections.property("name"));
       p1.add(Projections.property("salary"));       
       cr.setProjection(p1);
       List list = cr.list();
       Iterator itr = list.iterator();
       while(itr.hasNext()){
        Object ob[] = (Object[])itr.next();
          System.out.println(ob[0]+"--------"+ob[1]);
       }      
        session.close();
    }       

}

No comments:

Post a Comment