List of usage examples for org.hibernate StaleObjectStateException StaleObjectStateException
public StaleObjectStateException(String entityName, Serializable identifier)
From source file:com.axelor.db.JPA.java
License:Open Source License
private static <T extends Model> void checkVersion(T bean, Object version) { if (bean == null || version == null) { return;//ww w .ja v a2s .c om } final Class<T> klass = EntityHelper.getEntityClass(bean); final Model entity = JPA.em().find(klass, bean.getId()); if (entity == null || !Objects.equal(version, entity.getVersion())) { Exception cause = new StaleObjectStateException(klass.getName(), bean.getId()); throw new OptimisticLockException(cause.getMessage(), cause, bean); } }
From source file:com.axelor.db.JPA.java
License:Open Source License
/** * Verify the values against the database values to ensure the records * involved are not modified.//from w ww .ja va2 s. c o m * * @throws OptimisticLockException * if version mismatch of any a record is deleted */ @SuppressWarnings("all") public static void verify(Class<? extends Model> model, Map<String, Object> values) { if (values == null) { return; } Long id = null; try { id = Long.parseLong(values.get("id").toString()); } catch (Exception e) { } Object version = values.get("version"); Mapper mapper = Mapper.of(model); Model entity = id == null ? null : JPA.find(model, id); if (id != null && version != null) { if (entity == null || !Objects.equal(version, entity.getVersion())) { Exception cause = new StaleObjectStateException(model.getName(), id); throw new OptimisticLockException(cause); } } for (String key : values.keySet()) { Object value = values.get(key); Property property = mapper.getProperty(key); if (property == null || property.getTarget() == null) continue; if (!(value instanceof Map || value instanceof Collection)) { continue; } if (property.isCollection() && value instanceof Collection) { int size = 0; try { size = ((Collection) property.get(entity)).size(); } catch (Exception e) { } if (size > ((Collection) value).size()) { Exception cause = new StaleObjectStateException(model.getName(), id); throw new OptimisticLockException(cause); } } if (value instanceof Map) { value = Lists.newArrayList(value); } for (Object item : (Collection<?>) value) { if (item instanceof Map) { verify((Class) property.getTarget(), (Map) item); } } } }
From source file:com.axelor.db.JPA.java
License:Open Source License
@SuppressWarnings("all") private static <T extends Model> T _edit(Class<T> klass, Map<String, Object> values, Set<Model> visited, Multimap<String, Long> edited) { if (values == null) return null; Mapper mapper = Mapper.of(klass);/* w ww .j ava2 s . c o m*/ Long id = null; T bean = null; try { id = Long.valueOf(values.get("id").toString()); } catch (NumberFormatException e) { throw new IllegalArgumentException(e); } catch (NullPointerException e) { } if (id == null || id <= 0) { id = null; try { bean = klass.newInstance(); } catch (Exception ex) { throw new IllegalArgumentException(ex); } } else { bean = JPA.em().find(klass, id); if (bean == null) { throw new OptimisticLockException(new StaleObjectStateException(klass.getName(), id)); } } // optimistic concurrency check Integer beanVersion = (Integer) values.get("version"); boolean beanChanged = false; if (visited.contains(bean) && beanVersion == null) { return bean; } visited.add(bean); // don't update reference objects if (id != null && (beanVersion == null || edited.containsEntry(klass.getName(), id))) { return bean; } if (id != null) { edited.put(klass.getName(), id); } for (String name : values.keySet()) { Property p = mapper.getProperty(name); if (p == null || p.isPrimary() || p.isVersion() || mapper.getSetter(name) == null) continue; Object value = values.get(name); Class<Model> target = (Class<Model>) p.getTarget(); if (p.isCollection()) { Collection items = new ArrayList(); if (Set.class.isAssignableFrom(p.getJavaType())) items = new HashSet(); if (value instanceof Collection) { for (Object val : (Collection) value) { if (val instanceof Map) { if (p.getMappedBy() != null) { if (val instanceof ImmutableMap) val = Maps.newHashMap((Map) val); ((Map) val).remove(p.getMappedBy()); } Model item = _edit(target, (Map) val, visited, edited); items.add(p.setAssociation(item, bean)); } else if (val instanceof Number) { items.add(JPA.find(target, Long.parseLong(val.toString()))); } } } Object old = mapper.get(bean, name); if (old instanceof Collection) { boolean changed = ((Collection) old).size() != items.size(); if (!changed) { for (Object item : items) { if (!((Collection) old).contains(item)) { changed = true; break; } } } if (changed) { if (p.isOrphan()) { for (Object item : (Collection) old) { if (!items.contains(item)) { p.setAssociation(item, null); } } } p.clear(bean); p.addAll(bean, items); beanChanged = true; } continue; } if (p.getType() == PropertyType.MANY_TO_MANY && p.getMappedBy() != null) { p.addAll(bean, items); } value = items; } else if (value instanceof Map) { value = _edit(target, (Map) value, visited, edited); } Object oldValue = mapper.set(bean, name, value); if (p.valueChanged(bean, oldValue)) { beanChanged = true; } } if (beanChanged) { checkVersion(bean, beanVersion); } else if (id != null) { edited.remove(klass.getName(), id); } return bean; }
From source file:com.axelor.rpc.Resource.java
License:Open Source License
@Transactional @SuppressWarnings("all") public Response remove(Request request) { final Response response = new Response(); final Repository repository = JpaRepository.of(model); final List<Object> records = request.getRecords(); if (records == null || records.isEmpty()) { response.setException(new IllegalArgumentException("No records provides.")); return response; }//w w w .ja v a2 s . c o m final List<Model> entities = Lists.newArrayList(); for (Object record : records) { Map map = (Map) record; Long id = Longs.tryParse(map.get("id").toString()); Integer version = null; try { version = Ints.tryParse(map.get("version").toString()); } catch (Exception e) { } security.get().check(JpaSecurity.CAN_REMOVE, model, id); Model bean = JPA.find(model, id); if (version != null && !Objects.equal(version, bean.getVersion())) { throw new OptimisticLockException(new StaleObjectStateException(model.getName(), id)); } entities.add(bean); } for (Model entity : entities) { if (JPA.em().contains(entity)) { if (repository == null) { JPA.remove(entity); } else { repository.remove(entity); } } } response.setData(records); response.setStatus(Response.STATUS_SUCCESS); return response; }
From source file:com.utest.dao.UtestDeleteEventListener.java
License:Apache License
@SuppressWarnings("unchecked") @Override// w ww.j a v a 2 s . c o m /** * Handle the given delete event. This is the cascaded form. * * @param event The delete event. * @param transientEntities The cache of entities already deleted * * @throws HibernateException */ public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException { final EventSource source = event.getSession(); final PersistenceContext persistenceContext = source.getPersistenceContext(); Object entity = persistenceContext.unproxyAndReassociate(event.getObject()); EntityEntry entityEntry = persistenceContext.getEntry(entity); final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity); final Object version; if (persister.isVersioned()) { version = persister.getVersion(entity, source.getEntityMode()); // Make sure version has not changed on deleted entities if ((entity instanceof TimelineEntity) && !((TimelineEntity) entity).isNew()) { if (!persister.getVersionType().isEqual(version, entityEntry.getVersion())) { throw new StaleObjectStateException(persister.getEntityName(), entityEntry.getId()); } } } super.onDelete(event, transientEntities); }
From source file:com.utest.dao.UtestFlushEntityEventListener.java
License:Apache License
@Override public void onFlushEntity(final FlushEntityEvent event) throws HibernateException { final Object object = event.getEntity(); final EntityEntry entry = event.getEntityEntry(); final EntityPersister persister = entry.getPersister(); // final Status status = entry.getStatus(); final SessionImplementor session = event.getSession(); if (persister.isVersioned()) { final Object version = persister.getVersion(object, session.getEntityMode()); // Make sure version has not changed on updated entities if ((object instanceof TimelineEntity) && !((TimelineEntity) object).isNew()) { if (!persister.getVersionType().isEqual(version, entry.getVersion())) { throw new StaleObjectStateException(persister.getEntityName(), entry.getId()); }/* w ww . j ava 2 s .co m*/ } } super.onFlushEntity(event); }
From source file:fr.keyconsulting.oliphant.NotifyListener.java
License:Open Source License
public Serializable checkObject(Object object, EventSource session) throws StaleObjectStateException { Serializable identifier = session.getIdentifier(object); LOG.debug("* Checking object " + identifier + " : "); if (isKnownToBeStaleInSession(object, session)) { LOG.debug("Object is stale in session"); String entityName = session.getEntityName(object); if (isKnownToBeStaleInL2(object, session)) { LOG.debug(" and in L2 cache"); evictFromL2(object, session); }//from ww w .j av a 2 s .c o m throw new StaleObjectStateException(entityName, identifier); } LOG.debug("Object is not verifiably stale"); return null; }
From source file:org.libreplan.business.common.daos.GenericDAOHibernate.java
License:Open Source License
public void checkVersion(E entity) { /* Get id and version from entity */ Serializable id;//from w w w.j av a2 s.c o m Long versionValueInMemory; try { Method getIdMethod = entityClass.getMethod("getId"); id = (Serializable) getIdMethod.invoke(entity); if (id == null) { return; } Method getVersionMethod = entityClass.getMethod("getVersion"); versionValueInMemory = (Long) getVersionMethod.invoke(entity); if (versionValueInMemory == null) { return; } } catch (Exception e) { throw new RuntimeException(e); } /* Check version */ Long versionValueInDB = (Long) getSession().createCriteria(entityClass).add(Restrictions.idEq(id)) .setProjection(Projections.property("version")).uniqueResult(); if (versionValueInDB == null) { return; } if (!versionValueInMemory.equals(versionValueInDB)) { throw new StaleObjectStateException(entityClass.getName(), id); } }
From source file:org.openvpms.component.business.dao.hibernate.im.common.DOState.java
License:Open Source License
/** * Constructs a {@link DOState} for an object being assembled from an {@link IMObject}. * * @param object the object/*w w w . j ava 2 s . c o m*/ * @param source the source object */ public DOState(IMObjectDO object, IMObject source) { this.object = object; this.source = source; isNew = (source != null) && source.isNew(); version = (source != null) ? source.getVersion() : 0; if (!isNew && source != null) { if (source.getVersion() != object.getVersion()) { throw new StaleObjectStateException(object.getClass().getName(), object.getId()); } } }
From source file:org.openvpms.component.business.dao.hibernate.im.common.DOState.java
License:Open Source License
/** * Updates the state with a new instance of the source. * * @param source the new source instance * @throws StaleObjectStateException if the old and new versions aren't * the same *//*from w ww. j a v a 2s . c om*/ public void update(IMObject source) { if (source.getVersion() != object.getVersion()) { throw new StaleObjectStateException(object.getClass().getName(), object.getId()); } this.source = source; if (deferred != null) { deferred.clear(); } if (updaters != null) { if (reverters == null) { reverters = new HashMap<>(); } for (ReferenceUpdater updater : updaters) { if (!reverters.containsKey(updater.getReference())) { reverters.put(updater.getReference(), updater); } } updaters.clear(); } }