-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Oracle, Derby and createSQLQuery( )
PostPosted: Tue Sep 09, 2008 3:41 pm 
Newbie

Joined: Fri Jun 20, 2008 2:04 pm
Posts: 3
I wrote some code for Oracle that worked great. I was then asked to support the embedded Derby database using the same code. One inconsistent behavior I noticed is the Oracle driver returns a String object from a query while the Derby driver returns Character object. In a regular JDBC program, Derby returns a String object, which indicates the problem lies with Hibernate.

See the code below. Are there any work-arounds for this? I don't want to conditionally handle each database. Thanks!!!


/
Code:
********** SNIPPET FROM MY HIBERNATE PROJECT *************/

String sSQL = "select ft_name as NAME, 'ft' as AOTYPE from faulttree " +
                    "union " +
                "select dt_name as NAME, 'dt' as AOTYPE from decisiontree " +
                    "union " +
                "select exp_name as NAME, 'expr' as AOTYPE from expression " +
                    "union " +
                "select dt_type_name as NAME, 'dt_type' as AOTYPE from decisiontreetype";
       
AOType aoType = null;
         
List list = session.createSQLQuery(sSQL).list();
       
aoTypes = new HashMap(list.size());
       
for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
    Object[] obj = (Object[])iter.next();
           
    // obj[0] is a String object. Fine and dandy
   
    //----------------------- The problem is here! -------------------------
    // obj[1] is a String object using Oracle, but a Character object in   
    // Derby. Not good!
    //--------------------------------------------------------------------------
   
    System.out.println(obj[0] + " <<>> " + obj[1]);
}
   
/******************* JDBC TEST *********************/

package calc;

import java.sql.*;

public class JDBCTest {
  Connection conn;

  public static void main(String[] args) {
    new JDBCTest();
  }

  public JDBCTest() {
    try {
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
      String url = "jdbc:derby:MyTestDB;create=true";
      conn = DriverManager.getConnection(url, "", "");
      doTest();
      conn.close();
    }
    catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());}
    catch (IllegalAccessException ex) {System.err.println(ex.getMessage());}
    catch (InstantiationException ex) {System.err.println(ex.getMessage());}
    catch (SQLException ex)           {System.err.println(ex.getMessage());}
  }

  private void doTest() {
   
    System.out.println("TESTING SELECT");
    String sSQL = "select ft_name as NAME, 'ft' as AOTYPE from faulttree " +
                        "union " +
                    "select dt_name as NAME, 'dt' as AOTYPE from decisiontree " +
                        "union " +
                    "select exp_name as NAME, 'expr' as AOTYPE from expression " +
                        "union " +
                    "select dt_type_name as NAME, 'dt_type' as AOTYPE from decisiontreetype";
   
    try {
      Statement st = conn.createStatement();
      ResultSet rs = st.executeQuery(sSQL);
       
      while (rs.next()) {
        String s1 = rs.getString("NAME");
        String s2 = rs.getString("AOTYPE"); // <------- This works just fine!
        System.out.println(s1 + " <<>> " +  s2);
      }
    }
    catch (SQLException ex) {
      System.err.println(ex.getMessage());
    }
  }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.