For a project I’m working on, i’m adding import/export functionality. This is working OK, no issues.
What I’m trying to do now, is to have an option called “dryRun” which allows the user to test the import before actually doing anything, and just give an report back.
So, my setup is:
- Getting the import data through spring mvc (with OpenSessionInView)
- Deserialize this into my domain classes (which are the actual mapped hibernate classes)
- Scan through everything and make sure that references etc are ok.
- All my services have a @Transactional around them
This is mostly OK, but somewhere.. a save is triggered (i never call save), and my grouping object is complaining about a transient reference.. which is correct, since it is not saved.. I assume this is some kind of default mode hibernate has, since the grouping object references another object that is mapped.. So how can I make sure that hibernate never saves anything? without removing the @Transactional and OpenSessionInView (which is needed for the normal case)
Any ideas?
Regards,
Morten
You say
That “somewhere” is in the stack trace which you get with the exception.
[EDIT] The stack trace basically says “the method MetaDataController.importXml() is annotated with @Transactional”. When it returns, Hibernate tries to commit the transaction as it should and then, it stumbles over an unsaved dependency in
OrganisationUnitGroup(and yes, it would be nice if it would say which field contains the dependency because Hibernate knows this, too).There are several solutions:
Create two methods to import. The new one,
.importXmlDryRun()must not have the@Transactionalannotation. That might work but my gut feeling warns me about problems even though I couldn’t name them right now.Throw an exception in
.importXml()to abort the transaction when dry run is requested and ignore this exception in the default handler. The exception will cause Hibernate to roll back. This is the safest solution.Get the hibernate session and call
clear()(docs)