mirror of https://github.com/apache/openjpa.git
OPENJPA-245
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@614564 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8ef7997457
commit
bebd829b45
|
@ -98,7 +98,11 @@ class AttachManager {
|
|||
|
||||
CallbackException excep = null;
|
||||
try {
|
||||
return attach(pc, null, null, null, true);
|
||||
PersistenceCapable into = findFromDatabase(pc);
|
||||
OpenJPAStateManager owner = (into == null) ? null
|
||||
: (OpenJPAStateManager) into.pcGetStateManager();
|
||||
return attach(pc, into, owner, null, true);
|
||||
|
||||
} catch (CallbackException ce) {
|
||||
excep = ce;
|
||||
return null; // won't be reached as the exceps will be rethrown
|
||||
|
@ -335,4 +339,30 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ class VersionAttachStrategy
|
|||
meta.getRepository().getConfiguration());
|
||||
|
||||
boolean embedded = ownerMeta != null && ownerMeta.isEmbeddedPC();
|
||||
boolean isNew = !broker.isDetached(pc);
|
||||
boolean isNew = !broker.isDetached(pc) && into == null;
|
||||
Object version = null;
|
||||
StateManagerImpl sm;
|
||||
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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.simple;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
private int id;
|
||||
private String forename;
|
||||
private String surname;
|
||||
|
||||
@Id
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getForename() {
|
||||
return forename;
|
||||
}
|
||||
|
||||
public void setForename(String forename) {
|
||||
this.forename = forename;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
}
|
|
@ -18,6 +18,8 @@
|
|||
*/
|
||||
package org.apache.openjpa.persistence.simple;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import junit.textui.TestRunner;
|
||||
import org.apache.openjpa.persistence.test.SingleEMTestCase;
|
||||
|
||||
|
@ -30,7 +32,7 @@ public class TestEntityManagerMerge
|
|||
extends SingleEMTestCase {
|
||||
|
||||
public void setUp() {
|
||||
setUp(AllFieldTypes.class);
|
||||
setUp(AllFieldTypes.class, Person.class);
|
||||
}
|
||||
|
||||
public void testMerge() {
|
||||
|
@ -70,6 +72,42 @@ public class TestEntityManagerMerge
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* This test verifies that persisting a new entity which matches an existing
|
||||
* row in the database succeeds.
|
||||
*/
|
||||
public void testMergeExistingEntity() {
|
||||
Person p = new Person();
|
||||
p.setId(102);
|
||||
|
||||
EntityManager em = emf.createEntityManager();
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.persist(p);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
p = new Person();
|
||||
p.setId(102);
|
||||
p.setForename("Jane");
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.merge(p);
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
p = (Person) em.createQuery("Select p from Person p where p.id = 102")
|
||||
.getSingleResult();
|
||||
|
||||
assertNotNull(p);
|
||||
assertEquals("Jane", p.getForename());
|
||||
|
||||
em.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestRunner.run(TestEntityManagerMerge.class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue