-->
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: Truncate strings that are longer than their hbm length attr
PostPosted: Tue Oct 07, 2008 8:35 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
Is there an automated way to chop off strings that are too long when persisting in hibernate? For example:
Code:
    <property name="name" column="Name" length="255"/>

If I persist the class that contains the name field and it's >255 characters, I'll get an ugly exception:
Code:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'Name' at row 1


So I could truncate the name field in the setName setter, but that's a bit ugly as I'm truncating all the already truncated names when reading them from the database (what I'll mostly be doing). Additionally, I'll have to keep a maxLength field for each String property in all of my POJOs. Lastly, I don't want to restrict my POJOs necessarily this way, only when persisting them. Ideally there's an attribute for the property element called truncate="true", but I couldn't find this in any hibernate documentation.

What's the best way to automatically truncate Strings to their max hibernate specified length?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2008 5:43 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know if this is the best way, but using a UserType should work. See http://www.hibernate.org/hib_docs/v3/re ... pes-custom for an introduction.


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.