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
This commit is contained in:
Michael Dick 2008-02-01 23:03:05 +00:00
parent d5fed70238
commit c414e4cc79
2 changed files with 79 additions and 1 deletions

View File

@ -268,7 +268,10 @@ abstract class AttachStrategy
OpenJPAStateManager sm, ValueMetaData vmd) { OpenJPAStateManager sm, ValueMetaData vmd) {
if (toAttach == null) if (toAttach == null)
return 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); Object oid = manager.getDetachedObjectId(toAttach);
if (oid != null) if (oid != null)
return manager.getBroker().find(oid, false, null); return manager.getBroker().find(oid, false, null);

View File

@ -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);
}
}