-->
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.  [ 7 posts ] 
Author Message
 Post subject: Field validation before saving to database
PostPosted: Thu Feb 16, 2006 10:38 am 
Beginner
Beginner

Joined: Tue Jan 17, 2006 12:55 pm
Posts: 49
I am running a transaction to save an object to the database.
One of the properties is a string and I have set the length property in the mapping file.

I can set this property to a string of any length. However, when I try to persist it an exception is thrown on the call to transaction.Commit. It is a database exception telling me that the string is too long for the field.

Now, I would have expected NHibernate to have warned me before even giong to the database, since this information is in the mapping file.

Am I right to expect this or is there something I should do for this to happen?

Otherwise, are there any ways around this.. without testing the property in the class... since I do want to seperate out the database from the domain model, so I don't want to limit the field sizes in my classes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 2:21 pm 
Newbie

Joined: Tue Jun 06, 2006 2:18 pm
Posts: 2
Did you ever find a solution to this?

I too have the same concern and it seems "right" for hibernate to "represent" what it persists.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 7:54 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I believe that length field is only used when generating DDL from the hbm files.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 12:28 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
this is true. and to a certain extent your UI should perform some validation and constraint checking (imho).

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 4:09 am 
Beginner
Beginner

Joined: Tue Jan 17, 2006 12:55 pm
Posts: 49
devonl wrote:
this is true. and to a certain extent your UI should perform some validation and constraint checking (imho).

-devon


IMHO, the GUI should know nothing of the data storage system. Isn't that the whole point of uing OR Mapping?

My feeling is that the data constraint checking should be carried out by the OR Mapper, since this is the first point at which there is knowledge about the underlying data structure.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 8:02 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
The UI should perform length validation so you can immediately flag a field as being invalid and give the user instant feedback before pressing "submit" button, etc.

However, the data layer should also perform this validation to intelligently handle programmatic access to the data.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:13 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We handle this by having the string properties on our entities enforce the max length in their setter. We also decorate the property with a custom attribute that defines this max length, so that the UI can discover the max length and enforce it before actually trying to set the property. While this makes the max length redundant in two places, we don't have a problem with that because our entities are generated from the mapping files.


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