PDA

View Full Version : New or Edit state check on validation from controller


jmakeig
Aug 18th, 2004, 06:34 PM
The objects that I use as form-backing objects in my controllers don't get Id's until they are saved to the database. However, I'm having trouble validating from my controllers whether a null Id is an error or it's just an unsaved object. I don't really want to put an isNew method on my form-backing objects. Has anyone else overcome such an issue? Am I missing something?
Thanks in advance.

- Justin

oliverhutchison
Aug 18th, 2004, 06:39 PM
Insted of using null why not use 0 or -1 as the id for new objects? Then you can be sure that a null id is an exception.

jmakeig
Aug 18th, 2004, 06:43 PM
But then how do I determine if some other code has erroneously nulled (or zeroed, or negative-oned) my Id?

irbouho
Aug 18th, 2004, 07:09 PM
But then how do I determine if some other code has erroneously nulled (or zeroed, or negative-oned) my Id?

I would ask the same question about the logic behind isNew!!!

Hibernate (http://www.hibernate.org) uses the unsaved-value paradigm. If some code changes accidently the value of the object' id, this is the responsability of this code!!!

I think this is a clean and less-intrusive solution.

bobmanc
Aug 18th, 2004, 10:30 PM
You can add your isNew object to the model in the referenceData method so you won't need to include it in your command object.

mraible
Aug 18th, 2004, 11:25 PM
My id's are usually not exposed to the user, but rather as a hidden field in a form. When the record is new, the id of my POJO (a Long) is set to null - when it's an existing record, it's not. I doubt your issue is that simple, but it's worth a shot.