Wednesday, November 04, 2009

Hibernate flush only specific object types

So we have this issue where in certain cases we would only like to flush a certain class of objects and discard the rest. This is sort of a common use case if you are following the one session per request paradigm wherein if a job/request fails and the job status logging is also done via the database/hibernate then you would still want to persist the job log status however the application job data changes can be discarded. Found an excellent post here:

So as it turns out there is no way to selectively flush objects. You can use the "evict non flushable objects" methodology (basically bu evicting the objects their changes are discarded) but often times you don't know what all objects were involved in the request.

A better design is to probably open two sessions with each request/job. One for meta data and the other for application business data. That allows better handling of request failures.

1 comment:

  1. Anonymous2:35 AM

    This is a bit of a problem for me too.

    Interesting as just the other day I started looking at Ebean ORM ( which does not have session objects. Instead you just save() ... pretty intuitive and lets you just save what you want.

    Supports JPA annotations ... and cascades by reachablity by the looks.

    Could be worth a look and play.

    - Jim.