-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Automatically truncate String fields on updates?
PostPosted: Fri Oct 28, 2005 12:24 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
To avoid "value too long for column" errors, I would like Hibernate to automatically truncate any string fields before doing an insert or update using the values from the "length" parameter in the field definition of the mapping file of the object.

It doesn't seem to be doing this currently...I want to keep the java code auto-generatable, and I'd rather not write interceptors for each object. Seems like a pretty simple thing to do, so I'm probably just missing a setting somewhere...

Hibernate 2.x, by the way.


Top
 Profile  
 
 Post subject: oopsie
PostPosted: Fri Oct 28, 2005 1:07 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I used to have the same desire but after thinking about is for awhile I am convinced that H should not do that.
Think of a user who spent couple of hours typing a long description in a textArea with just one finger, and then tired pressed submit. Everything looked good as no error was displayed and he-or-she went to sleep satisfied (it was 1:30 AM by the way).

Next morning the person wants to add couple of sentences and…. Oopsie! Only first 100 characters were preserved…

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 1:26 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
I agree, applications should look at and handle the situation, but 90% of the time I just don't care. In most cases, I would rather it just truncated something instead of throwing a SQL exception (which, in my case, until this year of 2005 with Oracle 10g, it wouldn't even tell you which column).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is a FAQ!

http://hibernate.org/118.html#A17


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 2:09 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
That looks like it involves custom changes to the Java class, which I'm trying to avoid. Is there a way to autogenerate such a file, perhaps with meta tags?


Top
 Profile  
 
 Post subject: Configuration
PostPosted: Fri Oct 28, 2005 2:13 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Gavin, creating String50, String150 etc. does not look like good idea to me, especially because information about length can be specified already in the mapping files.

I could imagine a UserType that would take advantage of this information but it does not seem to be accessible through session. I mean that I did not notice a method that will lead to the configuration of the current session.
Did I overlook something?

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: Configuration
PostPosted: Fri Oct 28, 2005 5:48 pm 
Newbie

Joined: Wed Sep 14, 2005 12:17 pm
Posts: 13
kgignatyev wrote:
Gavin, creating String50, String150 etc. does not look like good idea to me, especially because information about length can be specified already in the mapping files.


I don't know how to access the length in the mapping file, but you can get away with creating one UserType that's parameterized, so you'd have something like this:

Code:
<property name="name" access="property">
   <column name="NAME"/>
   <type name="com.example.hibernate.types.MaxStringUserType">
      <param name="maximumLength">50</param>
   </type>
</property>


Ensure that you implement the ParameterizedType interface in your UserType.


Top
 Profile  
 
 Post subject: length definition
PostPosted: Fri Oct 28, 2005 5:56 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
http://www.hibernate.org/hib_docs/v3/re ... ing-column

Column element already defines length, or it could be specified for property

that means full definition in your example could look like :
<property name="name" access="property">
<column name="NAME" length="50"/>
<type name="com.example.hibernate.types.MaxStringUserType">
<param name="maximumLength">50</param>
</type>
</property>

So we have duplication of information which is BAD thing.

H uses length for DB schema generation and it make sense to make it available for reuse.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 8:24 pm 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
You almost certainly don't want automatic truncation of data by Hibernate. The right thing to do is to have a processing step on the object BEFORE it is saved. The steps would be:

1. Get input data from the user.
2. Validate the data, normally in a Servlet.
a) Validate the data individually, ie, the Street Address field must not be blank
b) Validate the data globally, ie, if the zipcode is 90210, and the city is New York, that is an error, even though the zipcode and city are valid individually.
3. After the data are validated, create or update the business object.
4. Commit the changes to the business object.

or, if the data were not valid, display a new page to the user giving information about what the errors were, re-display the data the user has already typed (so if one field out of twenty is wrong he doesn't have to type the other twenty), and let him try again.

That's really the process you have to go through.

Is there an automated way of handling some of these steps? It seems like there is a lot of repetition. For example I have to type in the property names in the HTML form, the Servlet that processes the form, the business object, and also in some JSP to display the result (in fact two JSPs, one for if the result is valid and one if it is not). Is there some "put it all in one place" solution? I have written a framework that uses introspection to handle all of this (ie, generate HTML forms from a business object, and a servlet to automatically process those forms). If there's nothing else out there that automates these steps I'm thinking of releasing my stuff as open source. Would there be interest?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 11:30 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
I'm wary of complicated frameworks, but considering all of this CRUD is the most annoying part of web development, I'd love to take a look.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 12:41 am 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
Tremelune wrote:
I'm wary of complicated frameworks, but considering all of this CRUD is the most annoying part of web development, I'd love to take a look.

Ok, I'm going to post it somewhere when I get it into reasonable shape.

It's not that complicated. The part about it that you will notice is that it has some inflexible naming conventions. In particular, if you have an object with a field called "foo" then the HTML form input has to have a name "foo". This ia actually a good thing because using naming conventions is the only realistic way you can avoid typing tons of meta-data to map one thing to another, and I hate all that.

The other thing it does is it uses some custom tags which are also capable of introspection. So let's say you are writing a web app to help a library manage its collection. You could then use a tag like this:

<SleepyBear:object name="book" field="title"/>

to show the title of a book. How does it do this? First it looks in the page scope for an object named "book". If it's not there, it looks in the request scope. If it's not there, it can (optionally) look in the request parameters for a parameter called bookId, which (if it is present and is an integer) it can use to load a Book object from Hibernate using session.get(). So those are the places it looks for the object.

Once it has found the object, it gets the title by finding the getTitle() method.

But it's even trickier than that. Let's say we want to do this:

<SleepyBear:object name="customer" field="address_city"/>

It will do the same things to find the Customer object. But there's a problem! Address is a component of Customer. There is no method Customer.getAddress_city(). What does it do? It actually notices that there is a Customer.getAddress() method and then notices that there is an Address.getCity() method and does the right thing. The joy of this is that you can then use your Address object all over the place and forms involving addresses will all work, without havng to retype stuff.

And on top of it all, it can generate your forms for you by doing the same introspection. It uses Java 5 annotations to give it hints in some cases.

So I'll post this. I hope there will be enough interest for people to test it out, because it will take me some amount of effort to post it and document it enough to be usable by others.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 12:15 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
gavin wrote:


"Use a UserType"

If the length field within a column isn't supposed to automatically truncate the field width or throw an error on afield value exceeding that length - in addition to the fact that the length attribute value is retrievable via ClassMetadata - exactly what is it's purpose?

It sounds suspiciously like it achieves absolutely nothing.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 12:17 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
damomurf wrote:
in addition to the fact that the length attribute value is retrievable via ClassMetadata


I of course meant to say the length attribute value isn't retrievable via ClassMetadata.


Top
 Profile  
 
 Post subject: length
PostPosted: Fri Nov 25, 2005 12:19 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
length is used for DDL generation

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: length
PostPosted: Fri Nov 25, 2005 12:21 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
kgignatyev wrote:
length is used for DDL generation


So, would it be correct to suppose then, that for someone using a legacy database schema, it achieves exactly nothing?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.