diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java index 931fdcd00..a1e7b873c 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java @@ -98,11 +98,7 @@ class AttachManager { CallbackException excep = null; try { - PersistenceCapable into = findFromDatabase(pc); - OpenJPAStateManager owner = (into == null) ? null - : (OpenJPAStateManager) into.pcGetStateManager(); - return attach(pc, into, owner, null, true); - + return attach(pc, null, null, null, true); } catch (CallbackException ce) { excep = ce; return null; // won't be reached as the exceps will be rethrown @@ -339,30 +335,4 @@ class AttachManager { Exceptions.toString(obj))).setFailedObject (obj); return sm; } - - /** - * Find a PersistenceCapable instance of an Object if it exists in the - * database. If the object is null or can't be found in the database. - * - * @param pc An object which will be attached into the current context. The - * object may or may not correspond to a row in the database. - * - * @return If the object is null or can't be found in the database this - * method returns null. Otherwise a PersistenceCapable representation of the - * object is returned. - */ - protected PersistenceCapable findFromDatabase(Object pc) { - PersistenceCapable rval = null; - - if (pc != null) { - Object oid = _broker.newObjectId(pc.getClass(), - getDetachedObjectId(pc)); - - if (oid != null) { - rval = ImplHelper.toPersistenceCapable(_broker.find(oid, true, - null), getBroker().getConfiguration()); - } - } - return rval; - } } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java index 8de18ed6f..b41f63c44 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java @@ -68,6 +68,13 @@ class VersionAttachStrategy public Object attach(AttachManager manager, Object toAttach, ClassMetaData meta, PersistenceCapable into, OpenJPAStateManager owner, ValueMetaData ownerMeta, boolean explicit) { + + // VersionAttachStrategy is invoked in the case where no more + // intelligent strategy could be found; let's be more lenient + // about new vs. detached record determination. + if (into == null) + into = findFromDatabase(manager, toAttach); + BrokerImpl broker = manager.getBroker(); PersistenceCapable pc = ImplHelper.toPersistenceCapable(toAttach, meta.getRepository().getConfiguration()); @@ -342,4 +349,29 @@ class VersionAttachStrategy } return (copy == null) ? map : copy; } + + /** + * Find a PersistenceCapable instance of an Object if it exists in the + * database. If the object is null or can't be found in the database. + * + * @param pc An object which will be attached into the current context. The + * object may or may not correspond to a row in the database. + * + * @return If the object is null or can't be found in the database this + * method returns null. Otherwise a PersistenceCapable representation of the + * object is returned. + */ + protected PersistenceCapable findFromDatabase(AttachManager manager, + Object pc) { + Object oid = manager.getBroker().newObjectId(pc.getClass(), + manager.getDetachedObjectId(pc)); + + if (oid != null) { + return ImplHelper.toPersistenceCapable( + manager.getBroker().find(oid, true, null), + manager.getBroker().getConfiguration()); + } else { + return null; + } + } }