Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More fixes for GRAILS-8988 , support for inserts that use primary key…

… that is given/generated by a sequence (=PostgreSql)

performSaveOrReplicate method has been changed to never delay inserts so that PreInsertEventListeners can modify the entity field values before inserting.
  • Loading branch information...
commit 9669e861d7007affdc760f3f32aac6154d36df3b 1 parent 6c359f8
@lhotari lhotari authored
View
66 .../src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/ClosureEventTriggeringInterceptor.java
@@ -191,9 +191,6 @@ protected Serializable performSaveOrReplicate(Object entity, EntityKey key, Enti
Serializable id = key == null ? null : key.getIdentifier();
- boolean inTxn = source.getJDBCContext().isTransactionInProgress();
- boolean shouldDelayIdentityInserts = !inTxn && !requiresImmediateIdAccess;
-
// Put a placeholder in entries, so we don't recurse back and try to save() the
// same object again. QUESTION: should this be done before onSave() is called?
// likewise, should it be done before onUpdate()?
@@ -212,10 +209,8 @@ protected Serializable performSaveOrReplicate(Object entity, EntityKey key, Enti
cascadeBeforeSave(source, persister, entity, anything);
- if (useIdentityColumn && !shouldDelayIdentityInserts) {
- log.trace("executing insertions");
- source.getActionQueue().executeInserts();
- }
+ log.trace("executing insertions");
+ source.getActionQueue().executeInserts();
Object[] values = persister.getPropertyValuesToInsert(entity, getMergeMap(anything), source);
Type[] types = persister.getPropertyTypes();
@@ -240,30 +235,24 @@ protected Serializable performSaveOrReplicate(Object entity, EntityKey key, Enti
new ForeignKeys.Nullifier(entity, false, useIdentityColumn, source)
.nullifyTransientReferences(values, types);
+ boolean insertVetoed = false;
+
if (useIdentityColumn) {
- isNullabilityCheckerAsLastEventListener(source);
EntityIdentityInsertAction insert = new EntityIdentityInsertAction(
- values, entity, persister, source, shouldDelayIdentityInserts);
- if (!shouldDelayIdentityInserts) {
- log.debug("executing identity-insert immediately");
- source.getActionQueue().execute(insert);
- id = insert.getGeneratedId();
- if (id != null) {
- // As of HHH-3904, if the id is null the operation was vetoed so we bail
- key = new EntityKey(id, persister, source.getEntityMode());
- source.getPersistenceContext().checkUniqueness(key, entity);
- }
- }
- else {
- log.debug("delaying identity-insert due to no transaction in progress");
- source.getActionQueue().addAction(insert);
- key = insert.getDelayedEntityKey();
+ values, entity, persister, source, false);
+ log.debug("executing identity-insert immediately");
+ source.getActionQueue().execute(insert);
+ id = insert.getGeneratedId();
+ if (id != null) {
+ // As of HHH-3904, if the id is null the operation was vetoed so we bail
+ key = new EntityKey(id, persister, source.getEntityMode());
+ source.getPersistenceContext().checkUniqueness(key, entity);
+ } else {
+ insertVetoed = true;
}
- } else {
- new Nullability(source).checkNullability(values, persister, false);
}
- if (key != null) {
+ if (!insertVetoed) {
Object version = Versioning.getVersion(values, persister);
source.getPersistenceContext().addEntity(
entity,
@@ -276,30 +265,27 @@ protected Serializable performSaveOrReplicate(Object entity, EntityKey key, Enti
persister,
isVersionIncrementDisabled(),
false);
- //source.getPersistenceContext().removeNonExist(new EntityKey(id, persister, source.getEntityMode()));
if (!useIdentityColumn) {
- source.getActionQueue().addAction(
- new EntityInsertAction(id, values, entity, version, persister, source));
+ EntityInsertAction insert = new EntityInsertAction(id, values, entity, version, persister, source);
+ source.getActionQueue().execute(insert);
+ if(!source.getPersistenceContext().wasInsertedDuringTransaction(persister, id)) {
+ insertVetoed=true;
+ }
}
- cascadeAfterSave(source, persister, entity, anything);
- // Very unfortunate code, but markInterceptorDirty is private. Once HHH-3904 is resolved remove this overridden method!
- if (markInterceptorDirtyMethod != null) {
- ReflectionUtils.invokeMethod(markInterceptorDirtyMethod, this, entity, persister, source);
+ if(!insertVetoed) {
+ cascadeAfterSave(source, persister, entity, anything);
+ // Very unfortunate code, but markInterceptorDirty is private. Once HHH-3904 is resolved remove this overridden method!
+ if (markInterceptorDirtyMethod != null) {
+ ReflectionUtils.invokeMethod(markInterceptorDirtyMethod, this, entity, persister, source);
+ }
}
}
return id;
}
- private void isNullabilityCheckerAsLastEventListener(EventSource source) {
- PreInsertEventListener[] preListeners = source.getListeners().getPreInsertEventListeners();
- if(preListeners==null || preListeners.length==0 || preListeners[preListeners.length-1]!=NULLABILITY_CHECKER_INSTANCE) {
- log.warn("Nullability check is not last PreInsertEventListener.");
- }
- }
-
public static final void addNullabilityCheckerPreInsertEventListener(EventListeners listenerRegistry) {
PreInsertEventListener[] preListeners = listenerRegistry.getPreInsertEventListeners();
Please sign in to comment.
Something went wrong with that request. Please try again.