tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Native SQL > Scalar Query

Scalar Query 

The following example shows how to execute native SQLs. It returns a List of Object arrays (Object[]) with scalar values for each column in the company table. Hibernate uses ResultSetMetadata to determine the order and types of the returned scalar values. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/native_queries/ScalarQueryTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.hibernate.basic.native_queries;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class ScalarQueryTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    Query query = session.createSQLQuery("SELECT * FROM company");
    List<?> rows = query.list();    
    System.out.println("Selected row count : " + rows.size());
    
    for (Object row : rows) {
      System.out.println(Arrays.toString((Object[])row));
    }
    
    session.getTransaction().commit();
    session.close();
  }
  
}
   

It gives the following output,
Selected row count : 10
[1, PQR, 3430, 220, add1@, add2]
[2, ABC, 23430, 160, add11, add22]
[3, MNO, 26790, 670, add111, add222]
[4, IJK, 67890, 850, add1112, add2223]
[5, ART, 67890, 850, add1112, add2223]
[6, RNK, 98890, 8478, add1412, add27823]
[7, AOQ, 97890, 4578, add17892, add2893]
[8, BQO, 9778, 487, add1459, add2443]
[9, CFR, 478, 2487, null, add2443]
[10, CFR, 6478, 887, , add24435]



 
  


  
bl  br