Originally posted by Lahori Munda:
But when I retrieve the object in the same method call i.e. after saving the record in the database I want to retrieve it back to display. The value of dateAdded remains null which causes NullPointerException during date formatting.
When you save an object, it doesn't read it back from the database. As well, it's now in the Session (unless you clear() it or open a new Session) and will be returned when read in the future. Finally, if you are using a second-level inter-Session cache, you'll read the old object without the timestamp from there instead of the database.
If you need to refresh the object with the database, which you need to do to pick up the database-generated timestamp field, you can use (IIRC, check the JavaDocs)
I'm new to Hibernate myself, so I don't know off the top of my head whether or not that bypasses the second level cache. I recall reading that it does, however.
Another option (I think refresh() does this) is to acquire a read lock on the object using lock(object, lockType).