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?