-->
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.  [ 13 posts ] 
Author Message
 Post subject: Query is returning trailing whitespace
PostPosted: Tue Jun 26, 2007 12:36 am 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Are there any hibernate properties that control whether leading/trailing whitespace is returned from queries?
My project was working fine, but now instead of returning (for example) "Jimbo" it is returning "Jimbo _____" where the excess whitespace (I had to use underlines - spaces were just being compressed) brings the return value size to the size of the column. Not sure what was changed to cause this.
I am using Hibernate3 wired from within the Spring Framework with an Oracle9i database, in case that matters.

-TIA[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 11:18 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
What is the datatype of the column? Sounds like you're using CHAR when you should use VARCHAR2.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 11:44 am 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Sure enough, it is now CHAR. I'm going to guess that it was changed unbeknownst to me at some point. Since I am not able to modify types (commercial application), is there any way to still work with CHAR?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 11:51 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
You will have to trim it in you application, preferably in the DAO. If you really wanted to bury this functionality inside Hibernate, you could map the property with a custom UserType or even possibly write an Interceptor.

It would be trivial, however, to just add a line of code in your application to handle this.

HTH,

Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 3:14 am 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
The only difficulty I have is that I am using the convenience methods, so for example a getRecord looks like nothing more than this:

Code:
return (MyObject) getHibernateTemplate().get( MyObject.class, thekey);


So I think making a custom type may be in order in order to continue using the convenience methods... do you have any experience with these?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 8:09 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
This forum post deals with this exact issue:
http://forum.hibernate.org/viewtopic.php?t=928294

I googled for trim string char site:hibernate.org and found it quite quickly :)

You actually may be able to extend StringType and touch only public Object get(ResultSet rs, String name), though there may be a catch here that I'm not thinking of.

To address your last post, though, you can still do:
Code:
MyObject myObject = (MyObject) getHibernateTemplate().get( MyObject.class, thekey);
if (myObject.getString() != null) {
  // trim it
}


But I like the UserType best and I think that's how most people will solve it.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 4:52 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
You have to take care that you don't trim() the field inside your application-code. If you do
Code:
MyObject myObject = (MyObject) getHibernateTemplate().get( MyObject.class, thekey);
if (myObject.getString() != null) {
  // trim it
}

hibernate will mark the Object as dirty and issue an unneccessary update-statement.
User-type is the way to do this!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 7:30 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Quote:
hibernate will mark the Object as dirty and issue an unneccessary update-statement. User-type is the way to do this!


Yes of course it will, thanks for pointing that out!

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 30, 2007 1:06 pm 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Does anyone have an example of implementing the UserType class on a recent (i.e. not 3 years ago ;-) ) version of Hibernate?
The example linked above is great, but it is missing several required function implementations - assemble, disassemble, hashcode, and replace.
The API documentation doesn't help me much on these.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 1:28 am 
Newbie

Joined: Fri Jun 29, 2007 9:03 am
Posts: 12
I hope ur porblem is solved already.If not here is what i have done for same problem.
to remove the trimmed string best way is to use UserType . U need to override some methods. I have written as code for the same problem :


Code:
public class NotNullString implements UserType {
    public TrimmedString() {
        super();
    }

    public int[] sqlTypes() {
        return new int[] { Types.CHAR };
    }

    public Class returnedClass() {
        return String.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        return (x == y) || (x != null && y != null && (x.equals(y)));
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        String val = rs.getString(names[0]);
        return val != null ? val.trim() : null;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        st.setString(index, (String)value);
    }

    public Object deepCopy(Object value) throws HibernateException {
        if (value == null) return null;
        return new String((String)value);
    }

    public boolean isMutable() {
        return false;
    }

    public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
        // TODO Auto-generated method stub
        return null;
    }

    public Serializable disassemble(Object arg0) throws HibernateException {
        return null;
    }

    public int hashCode(Object arg0) throws HibernateException {
        return 0;
    }

    public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
        return null;
    }
}

The nullSafeSet() and nullSafeGet() method is used to set and get the values from the database .


Last edited by rastogha on Tue Jul 03, 2007 1:49 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 8:51 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

always returning null or 0 doesn't seem like the best solution to me...
Here's what we have done concerning these methods (I don't know if we copied it from some other place, but here we are):
Code:
   public int hashCode(Object arg0) throws HibernateException {
      return arg0.hashCode();
   }

   public Serializable disassemble(Object arg0) throws HibernateException {
      return (Serializable) arg0;
   }

   public Object assemble(Serializable arg0, Object arg1)
         throws HibernateException {
      return (String) arg0;
   }

   public Object replace(Object arg0, Object arg1, Object arg2)
         throws HibernateException {
      return arg0;
   }

I don't know if this really is the way it's done, but at least it's working (as is rastogha's solution surely does too).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 8:51 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

always returning null or 0 doesn't seem like the best solution to me...
Here's what we have done concerning these methods (I don't know if we copied it from some other place, but here we are):
Code:
   public int hashCode(Object arg0) throws HibernateException {
      return arg0.hashCode();
   }

   public Serializable disassemble(Object arg0) throws HibernateException {
      return (Serializable) arg0;
   }

   public Object assemble(Serializable arg0, Object arg1)
         throws HibernateException {
      return (String) arg0;
   }

   public Object replace(Object arg0, Object arg1, Object arg2)
         throws HibernateException {
      return arg0;
   }

I don't know if this really is the way it's done, but at least it's working (as is rastogha's solution surely does too).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 10:48 am 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
OK, I've got the custom UserType in place, but it doesn't seem to be working at all... I'm not sure if it is because the mapping for this table is a bit awkward with composite ID. Here is the mapping:

Code:
   <class name="login.Users" table="USERS">
        <composite-id name="id" class="login.UsersId">
            <key-property name="username" type="login.TrimmedString">
                <column name="USERNAME" sql-type="char(12)" not-null="true"/>
            </key-property>
            <key-property name="password" type="string">
                <column name="PASSWORD" length="20" />
            </key-property>
        </composite-id>
        <property name="createdtadstp" type="timestamp">
            <column name="CREATEDTADSTP" length="7" />
        </property>
        <property name="modifiedtadstp" type="timestamp">
            <column name="MODIFIEDTADSTP" length="7" />
        </property>
    </class>


Even with the trimmed string type, if I try to enter username "ryan" it fails, instead I have to enter "ryan________" (spaces). Nothing in the POJO class (login.Users) needs to be changed, right?
Not sure what I'm missing. I am using Hibernate within the Spring Framework if that has any effect... Any ideas?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.