-->
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: Truncating strings to maximum field length
PostPosted: Wed Dec 07, 2005 7:22 pm 
Newbie

Joined: Wed Dec 07, 2005 7:07 pm
Posts: 4
Hi,

I was wondering if anybody knows how to get around the following error:

[JDBCExceptionReporter] ERROR: value too long for type character varying(50)

I am using Postgres 8.1 and Hibernate 3.0.5 with JBoss AS 4.0.2.

This happens as a result of trying to insert 51 characters (or more) into a varchar(50) field. MySQL and other databases truncate the data for you but Postgres will not.

I have tried setting the length attribute on the property mapping but it makes no difference. Here is the property mapping:

<property name="dropOffAddress2"
type="string"
update="true"
insert="true"
column="DropAdditionalAddress"
length="50"
not-null="false" />

The postgres doco says that you can explicitly truncate data by using the following syntax:

INSERT INTO test VALUES ('too long'::varchar(5));

Can anyone help me with a mapping to do this? I wish to avoid having to explicitly write code to trim the data to the correct length before insert or update.

Cheers!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 9:57 pm 
Newbie

Joined: Fri Dec 02, 2005 11:33 am
Posts: 10
Location: Québec, Canada
Hello,

You can probably write something like this:
<sql-insert>INSERT INTO test (x) VALUES (?::varchar(5))</sql-insert>

Look the Hibernate documentation for more details:
http://www.hibernate.org/hib_docs/v3/re ... erysql-cud

_________________
François J.
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 10:53 pm 
Newbie

Joined: Wed Dec 07, 2005 7:07 pm
Posts: 4
Yeah, Thanks, I thought of that. I was hoping for something a little more generic like some option to tell Hibernate to trim the data for you. Otherwise I'll have to write custom inserts for every mapping and then if a column definition changes I would have to go back and update the mappings. Yuck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:05 am 
Newbie

Joined: Fri Dec 02, 2005 11:33 am
Posts: 10
Location: Québec, Canada
Hello,

In last resort, you can use the Interceptors to trim the data before saving them. You would have to loop trough all the attributes of the entity and trim all the strings to the value you put the attribute's property. This way you will have something generic.

Documentation about the Interceptors:
http://www.hibernate.org/hib_docs/v3/re ... terceptors

Good Luck,

_________________
François J.
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:34 am 
Newbie

Joined: Wed Dec 07, 2005 7:07 pm
Posts: 4
After searching for something totally different I stumbled across the answer... Use a UserType.

It is actually a FAQ (oops) and there is a topic here.


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.