-->
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: Mapping for char datatype
PostPosted: Mon Apr 14, 2008 12:07 pm 
Newbie

Joined: Mon Apr 14, 2008 11:55 am
Posts: 4
Not actually a newbie, but never posted to the forum before, so I rate as a newbie here, I guess.

With Hibernate 3.1.2GA and Oracle 10g as the back-end. Also, with Spring as an IOC container, I am using HibernateDaoSupport as the main Hibernate interface.

I have an entity whose PK is a CHAR(64) in Oracle.

The ID column in the HBM.xml file is mapped as follows:
Code:
   <id
      name="id"
      type="string"
      column="PK_COLUMN_NAME"
   >
      <generator class="assigned"/>
   </id>



I found that mapping the ID property using the "string" hibernate type requires me to pad the query parameter with trailing spaces so the query sends a 64-length String as part of the query.

For example:
Code:
    Object[] params = { "Test" };
    String hqlString = "from ParentObject where childObject.id = ?";
    List<ParentObject> found = getHibernateTemplate().findByQuery(hqlString, params);
    // returns an empty list


However:
Code:
    Object[] params = { "Test[padded with 60 spaces]" };
    String hqlString = "from ParentObject where childObject.id = ?";
    List<ParentObject> found = getHibernateTemplate().findByQuery(hqlString, params);
    // returns a list populated with one row


Is there a different datatype mapping I could use so the padding is done automatically, or will I need to abstract the padding, or even possibly create a custom type?


Top
 Profile  
 
 Post subject: Possible answer
PostPosted: Mon Apr 14, 2008 12:25 pm 
Newbie

Joined: Mon Apr 14, 2008 11:55 am
Posts: 4
I think I may have found the answer on the hibernate site.

http://www.hibernate.org/388.html

Cool. Following the example provided in the link above, I had to make a minor tweak to get querying that object type to work with a String parameter to the query.

Additions to the code are as follows:

Code:
   private final int padLength = 64;
   
   /**
    * @see org.hibernate.usertype.UserType#nullSafeSet(PreparedStatement, Object, int)
    */
   public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i)
         throws HibernateException, SQLException {
      String val = padValue((String)o);
      inPreparedStatement.setString(i, val);
   }

   private String padValue(String valueToPad) {
      if( valueToPad == null ) {
         return valueToPad;
      }
      if( valueToPad.length() < getPadLength() ) {
         int startLength = valueToPad.length();
         for(int x=startLength; x<getPadLength(); x++) {
            valueToPad += " ";
         }
      }
      return valueToPad;
   }



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.