Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Hibernate
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
HQL from clause Example
In this example you will learn how to use the HQL from clause.
 
 

HQL from clause Example

                         

In this example you will learn how to use the HQL from clause. The from clause is the simplest possible Hibernate Query. Example of from clause is:

from Insurance insurance

Here is the full code of the from clause example:


 

 

 

 

package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;

import java.util.*;

/**
 @author Deepak Kumar
 *
 * http://www.roseindia.net
 * Select HQL Example
 */
public class SelectHQLExample {

  public static void main(String[] args) {
  Session session = null;

  try{
    // This step will read hibernate.cfg.xml
 and prepare hibernate for use

    SessionFactory sessionFactory = new 
Configuration
().configure().buildSessionFactory();
     session =sessionFactory.openSession();
     
     
 //Using from Clause
  String SQL_QUERY ="from Insurance insurance";
  Query query = session.createQuery(SQL_QUERY);
  for(Iterator it=query.iterate();it.hasNext();){
Insurance insurance=(Insurance)it.next();
 System.out.println("ID: " 
insurance.getLngInsuranceId
());
   System.out.println("First
 Name: " 
+ insurance.getInsuranceName());
   }
 
    session.close();
}catch(Exception e){
    System.out.println(e.getMessage());
}finally{
    }

  }  
}
 

To run the example select Run-> Run As -> Java Application from the menu bar. Following out is displayed in the Eclipse console:

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

log4j:WARN Please initialize the log4j system properly.

Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_

ID: 1

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Car Insurance

ID: 2

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Life Insurance

ID: 3

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Life Insurance

ID: 4

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Car Insurance

ID: 5

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Dental Insurance

ID: 6

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Life Insurance

ID: 7

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Travel Insurance

ID: 8

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Travel Insurance

ID: 9

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Medical Insurance

ID: 10

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Medical Insurance

ID: 11

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Home Insurance

ID: 12

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Home Insurance

ID: 13

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Motorcycle Insurance

ID: 14

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

First Name: Motorcycle Insurance

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

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

Current Comments

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

hi,
I have got the same problem "commons-beanutils.jar" which is referenced by classpath does not exists.wheter it is properly placed .

regards
ravi

Posted by ravi on Tuesday, 02.12.08 @ 17:46pm | #48030

Dear Deepak,

When i run this example i found an error "commons-beanutils.jar" which is refrenced by ur classpath , does not exist.Wheter it is properly their.

Pls tell me as soon as possible.

Posted by ravi on Tuesday, 02.12.08 @ 17:43pm | #48028

for someone can not execute you can insert
some line same below

Query query = session.createQuery(SQL_QUERY);
System.out.println("query===" + query.list());

when i am insert line query.list()) this class run successfull

Posted by star... on Sunday, 01.20.08 @ 15:21pm | #45677

After running above program I get shown below error.
Please let me know what needs to done.

Exception in thread "main" java.lang.NoClassDefFoundError: antlr/ANTLRException
at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:27)

at org.hibernate.impl.SessionFactoryImpl.createQueryTranslators(SessionFactoryImpl.java:344)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:410)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:814)
at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:850)
at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41)
at SelectHQLExample.main(SelectHQLExample.java:27)

Posted by Godwin Ajit on Friday, 01.11.08 @ 08:10am | #44948

Please send details about levels of hibernate

Posted by mahesh on Thursday, 11.29.07 @ 16:16pm | #40887

After running above program I didnt get any result just showing below lines.
Please let me know what needs to done.

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Before iterator
Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_

Posted by syed on Tuesday, 11.27.07 @ 15:02pm | #40693

from Insurance insurance

in the above line, after 'from' there are 2 insurance words not sure the reason for this ? can they differ if so when and how and why..?

Posted by Anil on Thursday, 11.8.07 @ 22:23pm | #36710

I need to learn Hybernet topics

Posted by vijaykumar on Wednesday, 08.22.07 @ 17:06pm | #23913

When I run above example. I am getting following:
----------------
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
*******************************
Query using Hibernate Query Language
Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_ where (insurance0_.ID='1')
could not execute query using iterate
------------------------------------

This is the same case for all the select queries. It always complain "Could not execute query using iterate". Can you please suggest me a solution?

Posted by Pradeep on Tuesday, 08.7.07 @ 02:44am | #22818

i want to insert data in table from a with intialization of a object. my class is mapped with another table

Posted by vivek jain on Wednesday, 07.25.07 @ 17:32pm | #21925

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  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

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.