From c414e4cc793729a26bd537cefb2333c86dda30c4 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Fri, 1 Feb 2008 23:03:05 +0000 Subject: [PATCH] OPENJPA-419 Ported to 1.0.x. git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@617695 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/kernel/AttachStrategy.java | 5 +- ...estMergeAndPersistWithManagedRelation.java | 75 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/lifecycle/TestMergeAndPersistWithManagedRelation.java diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachStrategy.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachStrategy.java index 15da014ca..028f43a1c 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachStrategy.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachStrategy.java @@ -268,7 +268,10 @@ abstract class AttachStrategy OpenJPAStateManager sm, ValueMetaData vmd) { if (toAttach == null) return null; - if (manager.getBroker().isDetached(toAttach)) { + + if (manager.getBroker().isPersistent(toAttach)) { + return toAttach; + } else if (manager.getBroker().isDetached(toAttach)) { Object oid = manager.getDetachedObjectId(toAttach); if (oid != null) return manager.getBroker().find(oid, false, null); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/lifecycle/TestMergeAndPersistWithManagedRelation.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/lifecycle/TestMergeAndPersistWithManagedRelation.java new file mode 100644 index 000000000..faa367e7a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/lifecycle/TestMergeAndPersistWithManagedRelation.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.lifecycle; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.persistence.relations.BidiChild; +import org.apache.openjpa.persistence.relations.BidiParent; +import org.apache.openjpa.persistence.test.SingleEMTestCase; + +public class TestMergeAndPersistWithManagedRelation + extends SingleEMTestCase { + + private long childId; + + public void setUp() { + setUp(BidiParent.class, BidiChild.class, CLEAR_TABLES); + + BidiChild child = new BidiChild(); + child.setName("child"); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(child); + em.getTransaction().commit(); + childId = child.getId(); + em.close(); + } + + public void testPersistWithManagedRelation() { + BidiParent parent = new BidiParent(); + BidiChild child = em.find(BidiChild.class, childId); + parent.setOneToOneChild(child); + em.getTransaction().begin(); + em.persist(parent); + em.getTransaction().commit(); + long id = parent.getId(); + em.close(); + + em = emf.createEntityManager(); + parent = em.find(BidiParent.class, id); + assertNotNull(parent); + } + + public void testMergeWithManagedRelation() { + BidiParent parent = new BidiParent(); + BidiChild child = em.find(BidiChild.class, childId); + parent.setOneToOneChild(child); + em.getTransaction().begin(); + parent = em.merge(parent); + em.getTransaction().commit(); + long id = parent.getId(); + em.close(); + + em = emf.createEntityManager(); + parent = em.find(BidiParent.class, id); + assertNotNull(parent); + } +}