Hi,
My system architecture is 3 tiers with long-lived conversation with session-per-request-with-detached-objects pattern.
So my objects is loaded in one session and saved in another session.
My application model is very complicate and includes a lot of objects graphs. The root object for each object graph declared with a version tag/field (the Customer in the example below).
If I want to ensure that when I'm saving a specific object graph, no another transaction will update the specific object graph, it's not enough to rely on the version field and I want to lock the record in the DB (select for update no wait) in the beginning of the transaction, before doing the business logic validations and saving the objects.
But when the object has dirty collections, I'm getting HibernateException: reassociated object has dirty collection.
How can I perform the locking of a dirty object?
An answer would be appreciated.
Yifat
Session session = null;
try {
// first request / session
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Customer customer = (Customer) session.load(Customer.class, new Long(1));
Hibernate.initialize(customer);
Hibernate.initialize(customer.getAddressList());
session.getTransaction().commit();
// changing the object outside the session
customer.addAddress(new Address("myAddress"));
// second request / session
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.lock(customer, LockMode.UPGRADE_NOWAIT);
session.saveOrUpdate(customer);
session.getTransaction().commit();
} catch (RuntimeException e) {
e.printStackTrace();
if(session != null) {
session.getTransaction().rollback();
}
}
|