-->
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.  [ 5 posts ] 
Author Message
 Post subject: Maxlength of String type
PostPosted: Thu Oct 26, 2006 7:45 am 
Newbie

Joined: Mon Oct 16, 2006 9:56 am
Posts: 10
[b]Hibernate version:1.2.0 beta1

[b]Name and version of the database you are using:SQL 2005

Hello all,

I have read a article some where about this..

there is a slight difference in what the two will do

<property type="String(50)" ... /> will create an IDbDataParameter that has
a length of 50 - so if you try to put a value of longer than 50 chars in
there it will truncate it.

<property type="String" length="50" .../> will create an IDbDataParameter
that has the max length of a nvarchar field in ms-sql - 4000. The attribute
length is in there for schema export and is not used to build the parameter.



I like this setting <property type="String(50)" ... />, which will truncate a value lnger than 50..

I write my mapping as following...

<property name="Name" access="property" type="String(32)">
<column name="Name" not-null="true"/>
</property>

and give a value than is longer than 50 characters...(it's supposed to truncate it and store to db, right?) There is a sql exception about a value is too long....why?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 7:47 am 
Newbie

Joined: Mon Oct 16, 2006 9:56 am
Posts: 10
even though I write the mapping in this way, it still won't truncate the string value...

<property name="Name" access="property" type="String(32)">
<column name="Name" not-null="true" sql-type="nvarchar(32)"/>
</property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 8:33 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It depends on the database, but silent truncation of values is dangerous so you should be glad MS SQL doesn't do it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 12:48 pm 
Newbie

Joined: Mon Oct 16, 2006 9:56 am
Posts: 10
would you mind that I am asking why that's dangerous??

In some scenarios, that's very useful..
I don't need to substring value of string type before insert to db..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 4:01 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It's dangerous because if you are using MySQL for example (it truncates string values), it's very easy for this truncation to happen when not intended, causing data loss that you might not even notice for some time.

If an exception happens when the data is oversized, you will notice it immediately. And there's a classic rule that says the earlier you detect a problem, the easier and less expensive it is to fix.


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