From 2a612c105315ff1b994a868505a65523e19690b0 Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Fri, 25 Jan 2008 19:41:57 +0000 Subject: [PATCH] OPENJPA-245 git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@615317 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/kernel/AttachManager.java | 32 +------------------ .../openjpa/kernel/VersionAttachStrategy.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 31 deletions(-) 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; + } + } }