mirror of
https://github.com/apache/openjpa.git
synced 2025-02-21 01:15:30 +00:00
OPENJPA-1983: Don't cascade to an unloaded field when merge is called on a managed Entity.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1095401 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
77b7ced1d5
commit
354eb3078e
@ -19,6 +19,7 @@
|
||||
package org.apache.openjpa.kernel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
@ -37,11 +38,11 @@ import org.apache.openjpa.meta.FieldMetaData;
|
||||
import org.apache.openjpa.meta.ValueMetaData;
|
||||
import org.apache.openjpa.util.CallbackException;
|
||||
import org.apache.openjpa.util.Exceptions;
|
||||
import org.apache.openjpa.util.ImplHelper;
|
||||
import org.apache.openjpa.util.OpenJPAException;
|
||||
import org.apache.openjpa.util.OptimisticException;
|
||||
import org.apache.openjpa.util.ProxyManager;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
import org.apache.openjpa.util.ImplHelper;
|
||||
|
||||
/**
|
||||
* Handles attaching instances.
|
||||
@ -59,7 +60,7 @@ public class AttachManager {
|
||||
private final boolean _copyNew;
|
||||
private final boolean _failFast;
|
||||
private final IdentityMap _attached = new IdentityMap();
|
||||
private final Collection _visitedNodes = new ArrayList();
|
||||
private final Collection<StateManagerImpl> _visitedNodes = new ArrayList();
|
||||
|
||||
// reusable strategies
|
||||
private AttachStrategy _version;
|
||||
@ -253,17 +254,18 @@ public class AttachManager {
|
||||
}
|
||||
|
||||
private Object handleCascade(Object toAttach, OpenJPAStateManager owner) {
|
||||
FieldMetaData[] fields = _broker.getStateManager(toAttach).getMetaData()
|
||||
.getDefinedFields();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
FieldMetaData fd = (FieldMetaData) fields[i];
|
||||
if (fd.getElement().getCascadeAttach() == fd.CASCADE_IMMEDIATE) {
|
||||
FieldMetaData[] inverseFieldMappings = fd.getInverseMetaDatas();
|
||||
StateManagerImpl sm = _broker.getStateManagerImpl(toAttach, true);
|
||||
BitSet loaded = sm.getLoaded();
|
||||
FieldMetaData[] fmds = sm.getMetaData().getDefinedFields();
|
||||
for (FieldMetaData fmd : fmds) {
|
||||
if (fmd.getElement().getCascadeAttach() == ValueMetaData.CASCADE_IMMEDIATE) {
|
||||
FieldMetaData[] inverseFieldMappings = fmd.getInverseMetaDatas();
|
||||
if (inverseFieldMappings.length != 0) {
|
||||
OpenJPAStateManager sm = _broker.getStateManager(toAttach);
|
||||
_visitedNodes.add(sm);
|
||||
getStrategy(toAttach).attachField(this, toAttach,
|
||||
_broker.getStateManagerImpl(toAttach, true), fd, true);
|
||||
// Only try to attach this field is it is loaded
|
||||
if (loaded.get(fmd.getIndex())) {
|
||||
getStrategy(toAttach).attachField(this, toAttach, sm, fmd, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.merge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.apache.openjpa.persistence.merge.model.Inner;
|
||||
import org.apache.openjpa.persistence.merge.model.Outer;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
public class TestLazyFields extends SingleEMFTestCase {
|
||||
public void setUp() {
|
||||
setUp(CLEAR_TABLES, Outer.class, Inner.class);
|
||||
}
|
||||
|
||||
public void testMergeOfLazyFields() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
try {
|
||||
em.getTransaction().begin();
|
||||
Outer o1 = new Outer();
|
||||
Inner i1 = new Inner();
|
||||
o1.setInners(new ArrayList<Inner>());
|
||||
o1.getInners().add(i1);
|
||||
|
||||
em.persist(o1);
|
||||
|
||||
em.getTransaction().commit();
|
||||
em.clear(); // the objects will now get detached.
|
||||
|
||||
long id = o1.getId();
|
||||
|
||||
em.getTransaction().begin();
|
||||
Outer o2 = em.find(Outer.class, id);
|
||||
|
||||
// Since o2 is in the context, it should be ignored... but the merge will needs to be cascaded
|
||||
// to loaded fields.
|
||||
Outer mergedO2 = em.merge(o2);
|
||||
|
||||
// Make sure that the merge operation didn't return a different outer.
|
||||
assertEquals(mergedO2, o2);
|
||||
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
|
||||
// Fetch again
|
||||
em.getTransaction().begin();
|
||||
Outer o3 = em.find(Outer.class, id);
|
||||
|
||||
// We're checking that the merge didn't cascade to the unloaded field and wipe out all Inners
|
||||
assertTrue(o3.getInners().size() > 0);
|
||||
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
|
||||
} finally {
|
||||
if (em != null) {
|
||||
if (em.getTransaction().isActive()) {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.merge.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "INNER_TBL")
|
||||
public class Inner {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id = null;
|
||||
|
||||
private String name = null;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "inners")
|
||||
private Collection<Outer> outers = new ArrayList<Outer>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Collection<Outer> getOuters() {
|
||||
return outers;
|
||||
}
|
||||
|
||||
public void setOuters(Collection<Outer> outers) {
|
||||
this.outers = outers;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.merge.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "OUTER_TBL")
|
||||
public class Outer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id = null;
|
||||
|
||||
private String name = null;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private List<Inner> inners = null;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Inner> getInners() {
|
||||
return inners;
|
||||
}
|
||||
|
||||
public void setInners(List<Inner> inners) {
|
||||
this.inners = inners;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user