Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

Hibernate Projections

                         

In this section, you will learn about the hibernate projections with an appropriate example.

Projections: The package Criteria is used as a framework by the applications just to build the new kinds of projection. may be used by applications as a framework for building new kinds of Projection. In general Projection means to retrieve while in case of SQL Projection means "Select" clause. Most of the applications uses the built-in projection types by means of the static factory methods of this class.

Product.java

package net.roseindia;

public class Product {
  
  private int id;
  private String name;
  private double price;
  private Dealer dealer;
  private int did;
  
  public Product(String name, double price) {
    super();
    // TODO Auto-generated constructor stub
    this.name = name;
    this.price = price;
  }
  public Product() {
    super();
    // TODO Auto-generated constructor stub
  }
  public Dealer getDealer() {
    return dealer;
  }
  public void setDealer(Dealer dealer) {
    this.dealer = dealer;
  }
  
  public double getDid() {
    return did;
  }
  public void setDid(int did) {
    this.did = did;
  }
  
  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 double getPrice() {
    return price;
  }
  public void setPrice(double price) {
    this.price = price;
  }

}

ProjectionList:  is the list of projection instances which are result of Query's object. 
Criteria API:  enables us to specify criteria based on various.

In the class projectionExample.java,  first we create the session object with the help of the SessionFactory interface. Then we use the createQuery() method of the Session object which returns a Query object. Now we use the openSession() method of the SessionFactory interface simply to instantiate the Session object.
Then we obtain the criteria object simply by invoking the createCriteria() method of the Session's object. Now we create a projectionList object add the fields having properties "name" and "price". Set it to the Criteria object by invoking the setProjection() method and passing the projectList object into this method and then add this object into the List interface's list object and iterate this object list object to display the data contained in this object.

projectionExample.java

package net.roseindia;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

public class projectionExample {

  /**
   @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    Session sess = null;
    try{
      SessionFactory sfact = new Configuration().configure().buildSessionFactory();
      sess = sfact.openSession();
      Criteria crit = sess.createCriteria(Product.class);
      ProjectionList proList = Projections.projectionList();
      proList.add(Projections.property("name"));
      proList.add(Projections.property("price"));
      crit.setProjection(proList);
      List list = crit.list();
      Iterator it = list.iterator();
      if(!it.hasNext()){
        System.out.println("No any data!");
      }
      else{
        while(it.hasNext()){
          Object[] row = (Object[])it.next();
          for(int i = 0; i < row.length;i++){
            System.out.print(row[i]);
            System.out.println();
          }
        }
      }
      sess.close();
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }

  }

}

Output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select this_.name as y0_, this_.price as y1_ from Product this_

Product Name      Price

Computer           23000.0

Mobile               15000.0

Laptop                200.0

Keyboard              1500.0

PenDrive                200.0

HardDisk          2500.0

Computer             100.0

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

Thanking your tutorials and servise

Posted by Harsha on Friday, 04.4.08 @ 10:46am | #55287

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.