-->
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.  [ 2 posts ] 
Author Message
 Post subject: NativeSQL SQLServer uniqueidentifier retrieved as Character
PostPosted: Mon Apr 14, 2008 3:53 pm 
Newbie

Joined: Mon Apr 14, 2008 1:33 pm
Posts: 3
This defect has been reported in other posts, but I have not been able to find documentation of action taken to rectify the problem. I attempt to provide a detailed scenario in hopes of finally putting this issue to rest.

There seems to be a problem when determining the return type of a uniqueidentifer column from Microsoft SQLServer 2000. We're using the JTDS 1.2.2 driver to connect to a MS SQLServer 2000 datasource. Our application allows for dynamic field generation based on the results of a SQL call. Most of the time this is used to populate name/value pairs of options (or as we call them "SelectItems") in a combo box, radio group, etc.

The native sql populates lists of SelectItems just fine if the values are "String" type columns (varchar, nvarchar, etc) in the datasource; however, if the configuration is set to select a value from a uniqueidentifier column from our datasource hibernate seems to type the result as a Character rather than a String of characters. The result, our combos' value properties contain only the first character of the selected GUID

We have gotten around this in other places by adding ScalarReturn objects to our SQLQuery object; however since these queries are read in on-the-fly we have no idea what columns to expect and therefore cannot use ScalarReturn objects.

It would seem that several "similar" mistyping issues have been reported in the forums:

http://forum.hibernate.org/viewtopic.ph ... identifier,
HHH-2304,
HHH-2934,
HHH-2220,
etc.

Hibernate's issue tracking however the problem still persists; I have done test builds of my application with Hibernate 3.2.6_GA to confirm that this issue still exists in the latest builds of Hibernate.

I have 2 questions:

1. Would it be advisable to create a new issue for this specific case?
2. Any suggestions on what to do in the interim?

Thank you for your time,
Aaron.

The following provides an example:

Relevant DDL:
Quote:
CREATE TABLE [dbo].[Client] (
[ClientGUID] uniqueidentifier ROWGUIDCOL NOT NULL,
[CompanyName] nvarchar(50) NULL,
...
)


Query:
Quote:
/* dynamic native SQL query */
SELEct
ClientGUID,
ClientName
FROM
Client
WHERE
TypeCode = '18'
ORDER BY
ClientName

Expected Results:
Quote:
DA7BE480-A8AB-B576-37F2-8F9DBBA4F6F5, Dr Xavier Agency
EE12796C-4891-3372-C835-C45A2A1248FE, Magneto Agency

Actual Results:
Quote:
D, Dr Xavier Agency
E, Magneto Agency


Hibernate version: 3.2.1_GA

Mapping documents: Annotation Based.

Code between sessionFactory.openSession() and session.close():

SelectItemDAO.java
Code:
   
   /**
    * Loads name value pairs matching the input sqlQueryString and builds a list of selectItemDcls for use in dynamic combo fields with the results
    *
    * @param sqlQueryString
    * @return
    */
   public List <SelectItemDcl> findSelectItemDclList( String sqlQueryString ) {
     
      List <Object[]> objectArrayList = executeSqlQuery( sqlQueryString );
    
     /* At this point objectArrayList contains:
      *  ----------------------------
      * |ClientGUID|ClientName      |
      * |---------------------------|
      * |D         |Dr Xavier Agency|
      * |E         |Magneto Agency  |
      *  ----------------------------
      *
      * However, we expect:
      *
      *  -------------------------------------------------------
      * |ClientGUID                           |ClientName      |
      * |------------------------------------------------------|
      * |DA7BE480-A8AB-B576-37F2-8F9DBBA4F6F5 |Dr Xavier Agency|
      * |EE12796C-4891-3372-C835-C45A2A1248FE |Magneto Agency  |
      *  -------------------------------------------------------
      *
      * when we make the following call to populateSelectItemDclList( objectArrayList )
      */

      List <SelectItemDcl> selectItemDclList = populateSelectItemDclList( objectArrayList );
      return selectItemDclList;
   }

   /**
    * Creates a list of SelectItemDcl objects from the input object array list
    *
    * @param objectArrayList List of object arrays. For each array, the value at index 0 represents the selectItem's value property and the value at index 1 represents the selectItem's text property
    * @return
    */
   public List <SelectItemDcl> populateSelectItemDclList( List <Object[]> objectArrayList ) {

      List <SelectItemDcl> selectItemDclList = null;
      SelectItemDcl selectItemDcl = null;
      if( objectArrayList != null && !objectArrayList.isEmpty() ) {
         selectItemDclList = new ArrayList <SelectItemDcl>();
         for( Object[] resultArray : objectArrayList ) {
            selectItemDcl = new SelectItemDcl();
            selectItemDcl.setValue( ( String )resultArray[0] ); //This is line 54; it is the origin of the java.lang.Character ClassCastException in the stacktrace
            selectItemDcl.setDisplay( ( String )resultArray[1] );
            selectItemDclList.add( selectItemDcl );
         }
      }
      return selectItemDclList;
   }


BaseDAO.java
Code:
   /**
    * Executes a Dynamic Native SQL Query
    *
    * @param sqlQueryString String representing the SQL Query to run
    * @return
    */
   @SuppressWarnings("unchecked")
   protected <T> List <T> executeSqlQuery( String sqlQueryString ) {

      SQLQuery query = getSession().createSQLQuery( sqlQueryString );
      List <T> list = query.list();
      return list;
   }


Full stack trace of any exception that occurs:

Quote:
Caused by: org.myapp.utl.exception.AsExceptionUtl: Failed to invoke method: "populateSelectItemDclList" in class "org.myapp.dal.hibernate.SelectItemDal" with arguments: [[[[D, Dr Xavier Agency],
[E, Magneto Agency]]].
at org.myapp.aop.ExceptionHandlerAspect.exceptionAdvice(ExceptionHandlerAspect.java:41)
at org.myapp.dal.hibernate.SelectItemDal.populateSelectItemDclList(SelectItemDal.java:46)
at org.myapp.dal.hibernate.SelectItemDal.findSelectItemDclList_aroundBody2(SelectItemDal.java:36)
at org.myapp.dal.hibernate.SelectItemDal$AjcClosure3.run(SelectItemDal.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:101)
at org.myapp.aop.ExceptionHandlerAspect.exceptionAdvice(ExceptionHandlerAspect.java:33)
... 115 more
Caused by: java.lang.ClassCastException: java.lang.Character
at org.myapp.dal.hibernate.SelectItemDal.populateSelectItemDclList_aroundBody4(SelectItemDal.java:54)

at org.myapp.dal.hibernate.SelectItemDal$AjcClosure5.run(SelectItemDal.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:101)
at org.myapp.aop.ExceptionHandlerAspect.exceptionAdvice(ExceptionHandlerAspect.java:33)
... 120 more



Name and version of the database you are using: MicroSoft SQLServer 2000

The generated SQL (show_sql=true):
Quote:
13:52:22,633 INFO [STDOUT] Hibernate:
/* dynamic native SQL query */ SELEct
ClientGUID,
ClientName
FROM
Client
WHERE
TypeCode = '18'
ORDER BY
ClientName


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 6:35 pm 
Newbie

Joined: Mon Apr 14, 2008 1:33 pm
Posts: 3
I've answered question #2 for myself and I thought I'd share, since others seem to be having the same problem.

To get around this issue I've done the following for the time being. I am requiring my users to provide a CAST on identifier columns when they write dynamic SQL.

For example instead of:

Quote:
SELECT
ClientGUID,
ClientName
FROM
Client
WHERE
TypeCode = '18'
ORDER BY
ClientName


The user will have to configure their dynamic query to cast the uniqueidentifier column as a "Character String" datatype:

Quote:
SELECT
CAST( ClientGUID AS VARCHAR(36) ),
ClientName
FROM
Client
WHERE
TypeCode = '18'
ORDER BY
ClientName


The problem is the user has to be aware of what columns in the datasource are uniqueidentifiers; however, we name these columns consistently so our users shouldn't have a problem.

I would still like to know if this issue is a valid defect as it seems a bit odd to me why the unique identifier would be interpreted as a Character rather than a String value.


Cheers,
Aaron


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

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.