hi
i'm writing a webapp here that keeps track of inventory items. normally, the id field has no business significance for the user (they don't even know about it), but for this app it's actually a good idea to make the id field important.
the user keeps track of what id numbers they need to be concerned with. on a page i have a text box, Find item by id#: ________
so they would come on and type in 6, a page loads item 6 from the db and shows all the stuff with it. but if they enter 439804343432432 in there, i get an ObjectNotFoundException, no row associated.
so, how do i handle that?
1. in the DAO.read(id) method, before it does the load: do a query, select id from Items where id = enteredId ... and see if that returns something. if so do the load, if not return null or something else. i would probably want to do some locking in here, what if the query says it exists, someone else deletes it real quick before they read it?
or
2. try / catch the exception .. i'd rather prevent the exception proactively, rather than catching it. this does save a trip to the database. also i've read something about the exception doesn't really happen until well after the load. so the DAO.read(id) method returns ok, but when i try to do returnedItem.getSomething() that's when the exception occurs ... that is really lousy.
better yet,
3. is there some sort of exists(id) method?
thoughts? anyone dealt with this before?
thanks!
|