-->
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: String padded with space
PostPosted: Thu Nov 27, 2003 12:14 pm 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
Hi everyone,

when loading object from MSSQL with Hibernate 2.0.3, the String attributes mapping to nvarchar type come with padded space at the end.

That is, if I got a NAME column of nvarchar(10) containing 'xxxx' , I'll get a String object containing: 'xxxx '

Could Hibernate be responsible for that?
Has anyone encountered the pb?

Help would be greatly appreciated ...

regards,
nodje


Last edited by nodje on Tue Jun 03, 2008 5:40 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
As I keep reminding everyone, Hibernate does not f@%$ with your strings. It is presumably a problem with your column definition / JDBC driver.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 12:23 pm 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
I don't think, that this depends on Hibernate. Have you checked, if the space is in the raw data at the database? (E.g. select '<'||name||'>' from foo)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 4:35 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
sorry to put it like that Gavin.
I supposed it doesn't come from Hibernate. Coz I get the same behavior when doing a Resultset.getString(column) (with DBUnit for instance)
But as Hibernate kind of 'encapsulate' JDBC I was living in the hope that I could maybe set an option from Hibernate to change the behavior.

It doesn't come from the DB itself. I checked by doing the adequate SQL call.
something is weird here though (i'll demonstrate with only one space char):
Say I enter the string 'test' in a nvarchar(5) column named COLUMN of a table TABLE. I do it via JDBC.
if I do a
Code:
select * from TABLE where COLUMN='test'
it works
Now if i retrieve the data via JDBC I'll got a 'test ' String (with the space due to nvarchar(5) column type) which is actually the pb i'm tryig to solve

Now I save this 'test ' data into the COLUMN with JDBC. (Hopefully I still got the same string when retrieving it.)

But then if I do a
Code:
select * from TABLE where COLUMN='test'
it works

if I do a
Code:
select * from TABLE where COLUMN='test '
it works also
?????

Is that a std DBMS bahavior or is it this MSSQL that is playing it differently?

I suspect it's the association of JSQLConnect and MSSQL. But i'm littlebit lost here coz it's like a black box.

So any help is still welcome

nodje


Last edited by nodje on Tue Jun 03, 2008 5:24 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 7:51 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
How many restults do you get, if you

Code:
select * from TABLE where COLUMN like 'test%'


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 9:29 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
Well, I get one result of course, since there's only one line with 'test' in the table...

What do you suggest?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 9:52 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
I hoped, that there are two rows in the table. One with COLUMN = 'test' and another one with COLUMN='test '. The only thing I could say is, that it is not a std behaviour of DBMS, that both queries give you the same result. I've no experience with MSSQL, but in Oracle or PostgreSQL only one of these queries would give you a result.

P.S.: Have you executed the queries without JDBC direct in a MSSQL console?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 10:34 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
Yes I did execute the queries direct into MSSQL console...

I just implemented a real JUnit test, creating a User object and then loading it and display it with a homemade toString().
I create it without space of course and get it with the space: except for the ID field, which sounds weird. Maybe it has to do with my mapping after all

here's the detail:

User.hbm.xml
------------------------
Code:
<hibernate-mapping>
<class name="org.xx.user.model.User" table="UTILISATEUR">
<id column="UTILISATEUR_CODE" name="id" type="java.lang.String">
<generator class="assigned"/>
</id>
<property column="UTILISATEUR_NOM" length="20" name="userNom" type="java.lang.String"/>
<property column="UTILISATEUR_PRENOM" length="20" name="userPrenom" type="java.lang.String"/>
<property column="UTILISATEUR_TELEPHONE" length="10" name="userTelephone" type="java.lang.String"/>
<property column="UTILISATEUR_EMAIL" length="40" name="userEmail" type="java.lang.String"/>
<property column="UTILISATEUR_SERVICE" length="30" name="userService" type="java.lang.String"/>
<property column="UTILISATEUR_PASSWORD" length="10" name="userPassword" type="java.lang.String"/>
<property column="UTILISATEUR_COMMENTAIRE" length="256" name="userCommentaire" type="java.lang.String"/>
<property column="UTILISATEUR_SIGNATURE" length="256" name="userSignature" type="java.lang.String"/>
<property column="UTILISATEUR_DATE_CREATION" length="23" name="userDateCreation" type="java.util.Date"/>
<property column="UTILISATEUR_DATE_SUPPRESSION" length="23" name="userDateSuppression" type="java.util.Date"/>
<property column="UTILISATEUR_ETAT_ACTIVATION" length="1" name="userEtatActivation" type="java.lang.Byte"/>
</class>
</hibernate-mapping>


BaseDAOHibernateTest.java
--------------------------------------
Code:
   protected void setUp() throws Exception {
      super.setUp();
      this.basedaohibernate = createInstance();
      this.user.setId("jxxxx");
      this.user.setUserNom("xxxx");
      this.user.setUserPrenom("xxxx");
      this.user.setUserTelephone("0140081261");
      this.user.setUserEmail("xxxx.xxxx@xx.com");
      this.user.setUserService("service xxxx");
      this.user.setUserPassword("xxxx");
      this.user.setUserCommentaire("comment: Ne fait pas parti de la maison. Consultant xxxx travaillant sur le projet xxxx (essai: �, �; �; �)");
      this.user.setUserSignature("c:'\'document'\'masignature.file");
      this.user.setUserDateCreation(this.todaysDate);
      this.user.setUserEtatActivation(Byte.valueOf("1"));
   }

public void testSaveObject() throws Exception {
      this.basedaohibernate.saveObject(this.user);
      assertEquals("l'ID du user sauve devrait etre 'jxxxx'. getID()="+this.user.getId(), this.user.getId(),"jxxxx");
      User usr = (User) this.basedaohibernate.retrieveObject(User.class,"jxxxx");
      assertEquals("les objet "+usr.toString()+"et "+ this.user.toString()+"devraient etre egaux...",usr, this.user);
      log.debug("retrievedUser : "+usr);
   }


the result:
----------------------------
Code:
15:26:35,343 DEBUG Service:114 - retrievedUser : org.xx.user.model.User
   id="jxxxx"
   userNom="xxxx              "
   userPrenom="xxxx                "
   userTelephone="0140081261"
   userEmail="xxxx.xxxx@xx.com                  "
   userService="service xxxx                "
   userPassword="xxxx      "
   userCommentaire="comment: Ne fait pas parti de la maison. Consultant xxxx travaillant sur le projet xxxx (essai: �, �; �; �)                                                                                                                                                 "
   userSignature="c:''document''masignature.file                                                                                                                                                                                                                                  "
   userDateCreation="2003-11-28 15:26:28.907"
   userDateSuppression="null"
   userEtatActivation="1"


Last edited by nodje on Tue Jun 03, 2008 5:42 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 11:00 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
Hm, if you got this result direct in the MSSQL console, than it seems to me, that MSSQL adds blanks to the result strings. Maybe you should read the MSSQL doku?!?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 29, 2003 8:37 pm 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
I have not ever worked with MSSQL but Informix does the same thing, if your fields are declared char instead of varchar. So if you have char (50) and varchar (50), the char field will be padded with spaces, the varchar will be trimed.

I ended up writing Interceptor to get rid of those spaces.

Alex.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:00 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
that's an interesting point.

I started study the conception of an interceptor at the DAO level, but it's rather complicated and expensive to trim all String attributes dynamically.

How did you implement it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 8:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is better to use a custom type.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 10:03 am 
Newbie

Joined: Tue Sep 30, 2003 5:46 am
Posts: 1
sounds good, but i'm not familiar with this.

The closer help topic i found about it is :
http://www.hibernate.org/hib_docs/refer ... pping-s2-4

I don't see how this could solve my space pb.

Could you give me a little bit more input please?


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.