Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [SOLVED] Native SQL and resulttransformer problem
PostPosted: Fri Dec 18, 2009 4:34 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 18
In our company we have a framework for paging. In a request object you pass page size, current page, filter and ordering,... and the query and in a response object you get the result as a List. The queries can be HQL as string, DetachedCriteria or native SQL as string.

Until now the requirement for the queries was that they returned a List<Object[]>.
I want to improve the framework so it can return a List of DTOs if a result class was set in the request object.

For HQL and Criteria queries it was easy: I call setResultTransformer(Transformers.aliasToBean(resultType)) and require that the HQL and the Criteria have aliases. It works perfectly.
For native SQL there is a problem: the aliases in the query are uppercased somewhere in the process so they don't map to the DTO's fields anymore.


Any idea how to tackle this problem?


I tried to dynamically add scalars to the SQLQuery using reflection but that didn't work because not all fields of the DTO are necessarily present as aliases in the query.


Last edited by NickDeGraeve on Mon Dec 21, 2009 5:14 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Native SQL and resulttransformer problem
PostPosted: Fri Dec 18, 2009 9:26 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 18
OK, I've got some progress.

I've created my own transformer based on AliasToBeanResultTransformer in which I use reflection to discover the correct case of the property.

This seems to take care of the naming issue but I'm not there yet as the types don't match in the return list.

I don't know how to get around that obstacle yet...


Relevant portion of SQLAliasToBeanResultTransformer:
Code:
   private Fields[] fields = resultClass.getDeclaredFields();

   public Object transformTuple(Object[] tuple, String[] aliases) {
      Object result;

      try {
         if ( setters == null ) {
            setters = new Setter[aliases.length];
            for ( int i = 0; i < aliases.length; i++ ) {
               String alias = aliases[i];
               if ( alias != null ) {
                  Setter setter;
                  try {
                     setter = propertyAccessor.getSetter(resultClass, alias);
                  } catch (PropertyNotFoundException e) {
                     for (Field field : this.fields) {
                        String fieldName = field.getName();
                        if (fieldName.equalsIgnoreCase(alias)) {
                           alias = fieldName;
                           break;
                        }
                     }
                     setter = propertyAccessor.getSetter(resultClass, alias);
                  }
                  setters[i] = setter;
               }
            }
         }
         result = resultClass.newInstance();

         for ( int i = 0; i < aliases.length; i++ ) {
            if ( setters[i] != null ) {
               setters[i].set( result, tuple[i], null );
            }
         }
      }
      catch ( InstantiationException e ) {
         throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
      }
      catch ( IllegalAccessException e ) {
         throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
      }

      return result;
   }


Top
 Profile  
 
 Post subject: Re: Native SQL and resulttransformer problem
PostPosted: Mon Dec 21, 2009 5:13 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 18
OK the wrong types were a bug present in the current implementation of the framework.
To make sure the correct types are returned, I added the method addColumnType(alias, type) to my SQLRequest class.

I think discovered a bug in Hibernate though. When I don't use a scalar one of my columns is returned as a single java.lang.Character instead of the string of the 2 characters it should be.
I reported it here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 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 2015, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.