diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCollections.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCollections.java index eee99d684..b5f54cb25 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCollections.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCollections.java @@ -38,11 +38,7 @@ public class ProxyCollections */ public static void beforeAdd(ProxyCollection coll, int index, Object value){ assertAllowedType(value, coll.getElementType()); - if (index == coll.size()) - // optimize for adding to the end - beforeAdd(coll, value); - else - dirty(coll, true); + dirty(coll, true); } /** diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/recursive/Node.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/recursive/Node.java new file mode 100644 index 000000000..2d2211d0e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/recursive/Node.java @@ -0,0 +1,67 @@ +/* + * 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.recursive; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Version; + +@Entity +public class Node { + + @Id + @GeneratedValue + private int id; + @Version + private int version; + + @OneToMany + private List nodes = new ArrayList(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public List getNodes() { + return nodes; + } + + public void setNodes(List nodes) { + this.nodes = nodes; + } + +} \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/recursive/TestRecursiveRelationships.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/recursive/TestRecursiveRelationships.java new file mode 100644 index 000000000..4db37279a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/recursive/TestRecursiveRelationships.java @@ -0,0 +1,81 @@ +/* + * 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.recursive; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestRecursiveRelationships extends SingleEMFTestCase { + private int _l1Nodes = 3; + private int _l2Nodes = 3; + + public void setUp() { + setUp(Node.class); + } + + public void testRecursiveNodes() { + EntityManager em = emf.createEntityManager(); + + // set up initial tree + em.getTransaction().begin(); + Node root = new Node(); + for (int i = 0; i < _l1Nodes; i++) { + Node n1 = new Node(); + root.getNodes().add(n1); + em.persist(n1); + } + em.persist(root); + em.getTransaction().commit(); + + // clear PC + em.refresh(root); + int rootId = root.getId(); + em.clear(); + em.close(); + em = emf.createEntityManager(); + + // add new nodes + em.getTransaction().begin(); + root = em.getReference(Node.class, rootId); + assertNotNull(root); + assertNotNull(root.getNodes()); + for (Node n : root.getNodes()) { + for (int j = 0; j < _l2Nodes; j++) { + Node n2 = new Node(); + n.getNodes().add(n.getNodes().size(), n2); + em.persist(n2); + } + } + em.getTransaction().commit(); + em.clear(); + em.close(); + em = emf.createEntityManager(); + + // ensure count is correct. + root = em.getReference(Node.class, rootId); + assertNotNull(root); + assertNotNull(root.getNodes()); + assertEquals(_l1Nodes, root.getNodes().size()); + for (Node n : root.getNodes()) { + assertEquals(_l2Nodes, n.getNodes().size()); + } + em.close(); + } +}