hibernate java curiosity - after saving the object, both the object to save and the saved one have id set -


i have following simple code:

@test public void saveexpense() {     // create dummy expense object i.e. { "description": "short description", "date": etc }      expense expensetosave = expensehelper.createexpense("short description", new date(), user);      expense savedexpense = expenseservice.save(expensetosave);      // strange, here, both expensetosave , savedexpense have id set 1 example; after save expense should have id;       expense expected = expensehelper.createexpense("short description", new date(), user);     // check if expected object equal saved 1     assert.asserttrue(expected.equals(expenseservice.findbydescription("short description"))); } 

normally expect expensetosave without id , savedexpense id, both have id after save. why?

that made variable necessary , complicate test.

thanks.

that's how hibernate session.save() method specified. documentation:

persist given transient instance, first assigning generated identifier. (or using current value of identifier property if assigned generator used.) operation cascades associated instances if association mapped cascade="save-update".

ids mechanism how hibernate differentiates between persisted , transient objects, , how identifies specific objects. therefore, id set in persistence step, example cyclic references in object tree resolved via ids while persisting.

what differentiates returned object vs. original object returned object attached hibernate session. example, active cascading, contained entities (e.g. in one-to-many collection) persistent instances in returned object.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -