Update BrokerImpl to skip check for duplicate ids when they are auto generated - merged Rick's changes from trunk

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@1084264 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2011-03-22 17:39:41 +00:00
parent 9b8b19f4e1
commit c20c89ab53
3 changed files with 103 additions and 2 deletions

View File

@ -2581,7 +2581,7 @@ public class BrokerImpl
}
// make sure we don't already have the instance cached
checkForDuplicateId(id, obj);
checkForDuplicateId(id, obj, meta);
// if had embedded sm, null it
if (sm != null)
@ -4952,7 +4952,11 @@ public class BrokerImpl
/**
* This method makes sure we don't already have the instance cached
*/
protected void checkForDuplicateId(Object id, Object obj) {
protected void checkForDuplicateId(Object id, Object obj, ClassMetaData meta) {
FieldMetaData[] pks = meta.getPrimaryKeyFields();
if (pks != null && pks.length == 1 && pks[0].getValueStrategy() == ValueStrategies.AUTOASSIGN) {
return;
}
StateManagerImpl other = getStateManagerImplById(id, false);
if (other != null && !other.isDeleted() && !other.isNew())
throw new ObjectExistsException(_loc.get("cache-exists",

View File

@ -0,0 +1,38 @@
/*
* 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.jdbc.auto;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class AutoIncrementEntity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int id;
public String somethingElse;
public int getId(){
return id;
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.jdbc.auto;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
public class TestAutoIncrement extends SingleEMTestCase {
public void setUp() {
super.setUp(DROP_TABLES, AutoIncrementEntity.class);
if (!((JDBCConfiguration) emf.getConfiguration()).getDBDictionaryInstance().supportsAutoAssign) {
return;
}
createZeroIdEntity();
}
public void test() {
em.getTransaction().begin();
AutoIncrementEntity e1 = em.find(AutoIncrementEntity.class, 0);
assertNotNull(e1);
AutoIncrementEntity e2 = new AutoIncrementEntity();
assertEquals(0, e2.getId());
em.persist(e2);
em.getTransaction().commit();
assertNotEquals(0, e2.getId());
}
/**
* A private worker method that will synthesize an Entity which has an auto generated id that starts at zero.
*/
private void createZeroIdEntity() {
em.getTransaction().begin();
AutoIncrementEntity aie = new AutoIncrementEntity();
em.persist(aie);
em.flush();
// If the created Entity has a non-zero id, update the Entity to have a zero id.
if (aie.getId() != 0) {
em.createQuery("UPDATE AutoIncrementEntity a SET a.id = 0 WHERE a.id = :id")
.setParameter("id", aie.getId()).executeUpdate();
}
em.getTransaction().commit();
}
}