OPENJPA-885 Committing code and tests contributed by Dianne Richards.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@755468 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2009-03-18 04:01:04 +00:00
parent ff8d46578c
commit 65a8219817
18 changed files with 1389 additions and 3 deletions

View File

@ -0,0 +1,68 @@
/*
* 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.detach;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Entity1 {
@Id
private long id;
private String name;
@OneToOne(cascade=(CascadeType.PERSIST))
private Entity14 e14;
public long getId() {
return id;
}
public Entity1() {
}
public Entity1(long id, String name) {
this.id = id;
this.name = name;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Entity14 getE14() {
return e14;
}
public void setE14(Entity14 e14) {
this.e14 = e14;
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.detach;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Entity10 {
@Id
private long id;
private String name;
@OneToMany(cascade=(CascadeType.ALL))
private Collection<Entity8> collection;
private Collection<Integer> intCollection;
@ElementCollection
private Collection<String> stringCollection;
public Entity10() {
initialize();
}
public Entity10(long id, String name) {
this.id = id;
this.name = name;
initialize();
}
private void initialize() {
intCollection = new ArrayList<Integer>();
intCollection.add(new Integer(1));
intCollection.add(new Integer(99));
stringCollection = new HashSet<String>();
stringCollection.add(new String("xxx"));
stringCollection.add(new String("yyy"));
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Entity8> getCollection() {
return collection;
}
public void setCollection(Collection<Entity8> collection) {
this.collection = collection;
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.detach;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Entity11 {
@Id
private long id;
private String name;
public Entity11() {
}
public Entity11(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.detach;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Entity12 {
@Id
private long id;
private String name;
public Entity12() {
}
public Entity12(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -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.detach;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
@Entity
public class Entity13 {
@Id
private long id;
private String name;
@MapKey(name = "name")
@OneToMany(cascade=(CascadeType.ALL))
private Map<String, Entity11> map;
public Entity13() {
}
public Entity13(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Entity11> getMap() {
return map;
}
public void setMap(Map<String, Entity11> map) {
this.map = map;
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.detach;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Entity14 {
@Id
private long id;
private String name;
public Entity14() {
}
public Entity14(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -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.detach;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Entity3 {
@Id
private long id;
private String name;
@OneToOne(cascade=(CascadeType.ALL))
private Entity4 e4;
public Entity3() {
}
public Entity3(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Entity4 getE4() {
return e4;
}
public void setE4(Entity4 e4) {
this.e4 = e4;
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.detach;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Entity4 {
@Id
private long id;
private String name;
@OneToOne(cascade=(CascadeType.CLEAR))
private Entity5 e5;
public Entity4() {
}
public Entity4(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Entity5 getE5() {
return e5;
}
public void setE5(Entity5 e5) {
this.e5 = e5;
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.detach;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Entity5 {
@Id
private long id;
private String name;
@OneToOne
private Entity6 e6;
public Entity5() {
}
public Entity5(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Entity6 getE6() {
return e6;
}
public void setE6(Entity6 e6) {
this.e6 = e6;
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.detach;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Entity6 {
@Id
private long id;
private String name;
@OneToOne(cascade=(CascadeType.CLEAR))
private Entity7 e7;
public Entity6() {
}
public Entity6(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Entity7 getE7() {
return e7;
}
public void setE7(Entity7 e7) {
this.e7 = e7;
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.detach;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Entity7 {
@Id
private long id;
private String name;
public Entity7() {
}
public Entity7(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.detach;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Entity8 {
@Id
private long id;
private String name;
@OneToOne(cascade=(CascadeType.ALL))
private Entity9 e9;
public Entity8() {
}
public Entity8(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Entity9 getE9() {
return e9;
}
public void setE9(Entity9 e9) {
this.e9 = e9;
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.detach;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Entity9 {
@Id
private long id;
private String name;
public Entity9() {
}
public Entity9(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,301 @@
/*
* 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.detach;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.apache.openjpa.conf.Compatibility;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.meta.ValueMetaData;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestDetachCascade extends SingleEMFTestCase {
OpenJPAEntityManager em;
int id = 0;
Compatibility compat;
Entity1 e1; // references e14 - no cascade
Entity3 e3; // references e4 - cascade ALL
Entity4 e4; // references e5 - cascade CLEAR
Entity5 e5; // references e6 - no cascade
Entity6 e6; // references e7 - cascade CLEAR
Entity7 e7;
Entity8 e8; // references e9 - cascade ALL
Entity8 e8a; // same as e8
Entity9 e9;
Entity10 e10; // references a Collection<Entity8> - cascade ALL
Entity11 e11;
Entity12 e12;
Entity13 e13; // references a Map<String, Entity11> - cascade ALL
Entity14 e14;
Collection<Object> allEntities = new HashSet<Object>();
public void setUp() throws Exception {
setUp(Entity1.class,
Entity3.class,
Entity4.class,
Entity5.class,
Entity6.class,
Entity7.class,
Entity8.class,
Entity9.class,
Entity10.class,
Entity11.class,
Entity12.class,
Entity13.class,
Entity14.class);
assertNotNull(emf);
em = emf.createEntityManager();
assertNotNull(em);
compat = emf.getConfiguration().getCompatibilityInstance();
assertNotNull(compat);
}
private void create(int id) {
allEntities.clear();
e1 = new Entity1(id, "entity1");
allEntities.add(e1);
e3 = new Entity3(id, "entity3");
allEntities.add(e3);
e4 = new Entity4(id, "entity4");
allEntities.add(e4);
e5 = new Entity5(id, "entity5");
allEntities.add(e5);
e6 = new Entity6(id, "entity6");
allEntities.add(e6);
e7 = new Entity7(id, "entity7");
allEntities.add(e7);
e8 = new Entity8(id, "entity8");
allEntities.add(e8);
e8a = new Entity8(id + 100, "entity8a");
allEntities.add(e8a);
e9 = new Entity9(id, "entity9");
allEntities.add(e9);
e10 = new Entity10(id, "entity10");
allEntities.add(e10);
e11 = new Entity11(id, "entity11");
allEntities.add(e11);
e12 = new Entity12(id, "entity12");
allEntities.add(e12);
e13 = new Entity13(id, "entity13");
allEntities.add(e13);
e14 = new Entity14(id, "entity14");
allEntities.add(e14);
e1.setE14(e14);
e3.setE4(e4);
e4.setE5(e5);
e5.setE6(e6);
e6.setE7(e7);
e8.setE9(e9);
Collection<Entity8> collection = new HashSet<Entity8>();
collection.add(e8);
collection.add(e8a); // e8a contains a null value for Entity9
e10.setCollection(collection);
Map<String, Entity11> map = new HashMap<String, Entity11>();
map.put(e11.getName(), e11);
e13.setMap(map);
}
// Test that detach cascade values are set correctly
public void testCascadeValues() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
em.getTransaction().commit();
MetaDataRepository repos = emf.getConfiguration()
.getMetaDataRepositoryInstance();
// Test CascadeType.ALL
ClassMetaData meta3 = repos.getCachedMetaData(Entity3.class);
assertNotNull(meta3);
assertEquals(ValueMetaData.CASCADE_IMMEDIATE,
meta3.getField("e4").getCascadeDetach());
// Test CascadeType.CLEAR
ClassMetaData meta4 = repos.getCachedMetaData(Entity4.class);
assertNotNull(meta4);
assertEquals(ValueMetaData.CASCADE_IMMEDIATE,
meta4.getField("e5").getCascadeDetach());
}
// Make sure cascade is no longer done by default
public void testNoCascade() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e1));
assertTrue(em.contains(e14));
em.clear(e1);
assertFalse(em.contains(e1));
assertTrue(em.contains(e14));
em.getTransaction().commit();
}
// Change to the previous behavior to always cascade
public void testAlwaysCascade() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e1));
assertTrue(em.contains(e14));
compat.setCascadeWithDetach(true);
em.clear(e1);
assertFalse(em.contains(e1));
assertFalse(em.contains(e14));
em.getTransaction().commit();
// reset compatibility option to default
compat.setCascadeWithDetach(false);
}
// Test explicit cascade of entities
public void testCascadeOfEntities() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e3));
assertTrue(em.contains(e4));
em.clear(e3);
assertTrue(em.contains(e1));
assertFalse(em.contains(e3));
assertFalse(em.contains(e4));
assertFalse(em.contains(e5));
assertTrue(em.contains(e6));
assertTrue(em.contains(e7));
em.getTransaction().commit();
}
// Test always cascade of entities
public void testAlwaysCascadeOfEntities() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e3));
assertTrue(em.contains(e4));
compat.setCascadeWithDetach(true);
em.clear(e3);
assertTrue(em.contains(e1));
assertFalse(em.contains(e3));
assertFalse(em.contains(e4));
assertFalse(em.contains(e5));
assertFalse(em.contains(e6));
assertFalse(em.contains(e7));
em.getTransaction().commit();
compat.setCascadeWithDetach(false);
}
// test single cascade in new transaction with no fetch of e4
public void testSingleCascadeNoFetch() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e3));
assertTrue(em.contains(e4));
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
em.clear();
e3 = em.find(Entity3.class, id);
assertTrue(em.contains(e3));
assertFalse(em.contains(e4));
em.getTransaction().commit();
em.clear(e3);
assertFalse(em.contains(e3));
assertFalse(em.contains(e4));
}
// test cascade of a simple collection
public void testCascadeOfCollection() {
id++;
em.getTransaction().begin();
create(id);
em.persist(e10);
assertTrue(em.contains(e10));
assertTrue(em.contains(e8));
assertTrue(em.contains(e9));
assertTrue(em.contains(e8a));
em.clear(e10);
assertFalse(em.contains(e10));
assertFalse(em.contains(e8));
assertFalse(em.contains(e9));
assertFalse(em.contains(e8a));
em.getTransaction().commit();
}
// test cascade of Map
public void testCascadeOfMap() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e11));
assertTrue(em.contains(e13));
em.clear(e13);
assertFalse(em.contains(e13));
assertFalse(em.contains(e11));
em.getTransaction().commit();
}
// Test old detach behavior - flush, copy, and cascade
public void testOldDetachBehavior() {
id++;
compat.setCascadeWithDetach(true);
compat.setCopyOnDetach(true);
compat.setFlushBeforeDetach(true);
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
Entity1 e1Det = em.detach(e1);
assertNotEquals(e1, e1Det); // check for copy
Entity14 e14Det = e1.getE14();
assertEquals(e14, e14Det);
em.getTransaction().commit();
// check for flushed and cascaded flushed
Entity1 e1Saved = em.find(Entity1.class, id);
assertNotNull(e1Saved);
Entity14 e14Saved = e1Saved.getE14();
assertNotNull(e14Saved);
compat.setCascadeWithDetach(false);
compat.setCopyOnDetach(false);
compat.setFlushBeforeDetach(false);
}
}

View File

@ -0,0 +1,259 @@
/*
* 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.detach;
import java.util.Collection;
import java.util.HashSet;
import org.apache.openjpa.conf.Compatibility;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.meta.ValueMetaData;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestDetachNoCascade extends SingleEMFTestCase {
OpenJPAEntityManager em;
int id = 0;
Compatibility compat;
Entity1 e1;
Entity7 e7;
Entity14 e14;
Collection<Object> allEntities = new HashSet<Object>();
public void setUp() throws Exception {
setUp(Entity1.class, // references Entity14
Entity7.class,
Entity14.class,
CLEAR_TABLES);
assertNotNull(emf);
em = emf.createEntityManager();
assertNotNull(em);
compat = emf.getConfiguration().getCompatibilityInstance();
}
private void create(int id) {
allEntities.clear();
e1 = new Entity1(id,"entity1");
allEntities.add(e1);
e7 = new Entity7(id, "entity7");
allEntities.add(e7);
e14 = new Entity14(id, "entity14");
allEntities.add(e14);
e1.setE14(e14);
}
// Make sure the default values are now changed
public void testChangedDefaults() {
assertFalse(compat.getFlushBeforeDetach());
assertFalse(compat.getCopyOnDetach());
assertFalse(compat.getCascadeWithDetach());
}
// Make sure all the fields in Entity1 are CASCADE_NONE
public void testCascadeValues() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
em.getTransaction().commit();
MetaDataRepository repos = emf.getConfiguration()
.getMetaDataRepositoryInstance();
ClassMetaData meta = repos.getCachedMetaData(Entity1.class);
assertNotNull(meta);
assertEquals(ValueMetaData.CASCADE_NONE, meta.getField("id").getCascadeDetach());
assertEquals(ValueMetaData.CASCADE_NONE, meta.getField("name").getCascadeDetach());
assertEquals(ValueMetaData.CASCADE_NONE, meta.getField("e14").getCascadeDetach());
}
// Test clear() to clear all entities.
public void testClearAll() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertEquals(allEntities.size(),em.getManagedObjects().size());
em.clear();
assertEquals(0,em.getManagedObjects().size());
assertFalse(em.contains(e1));
em.getTransaction().commit();
}
// Test clear(Object entity) to clear 1 entity. Do not
// cascade and make sure contained entities and other entities
// are still managed.
public void testClearOne() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
assertTrue(em.contains(e1));
em.clear(e1);
assertFalse(em.contains(e1));
assertTrue(em.contains(e14));
assertTrue(em.contains(e7));
em.getTransaction().commit();
}
// Test clear on a new, unmanaged object. Nothing should happen. After
// persist it should still become a managed entity.
public void testClearNew() {
id++;
em.getTransaction().begin();
create(id);
assertFalse(em.contains(e1));
em.clear(e1);
em.persistAll(allEntities);
assertTrue(em.contains(e1));
em.getTransaction().commit();
}
// Test clear on dirty object. Make sure the change is not flushed.
public void testClearDirty() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
em.getTransaction().commit();
em.getTransaction().begin();
e1 = em.find(Entity1.class, id);
assertEquals("entity1",e1.getName());
e1.setName("new name");
em.clear(e1);
em.getTransaction().commit();
em.getTransaction().begin();
em.clear();
e1 = em.find(Entity1.class, id);
assertNotEquals("new name", e1.getName());
assertEquals("entity1", e1.getName());
em.getTransaction().commit();
}
// Remove an Entity before clearing it. Make sure it is still in the
// DB after the commit.
public void testClearRemove() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
em.getTransaction().commit();
em.getTransaction().begin();
e1 = em.find(Entity1.class, id);
em.remove(e1);
em.clear(e1);
em.getTransaction().commit();
em.getTransaction().begin();
em.clear();
e1 = em.find(Entity1.class, id);
assertNotNull(e1);
em.getTransaction().commit();
}
// Try to clear an entity that has already been cleared. There should be
// no exception and the entity should still not be there.
public void testClearOnClearedEntity() {
id++;
em.getTransaction().begin();
create(id);
em.persist(e1);
em.clear(e1);
em.clear(e1);
assertFalse(em.contains(e1));
em.getTransaction().commit();
}
// Test that no copy is done by default
public void testNoCopy() {
id++;
em.getTransaction().begin();
create(id);
em.persist(e1);
Entity1 e1Det = em.detach(e1);
assertEquals(e1, e1Det);
assertFalse(em.contains(e1));
assertTrue(em.contains(e14));
em.getTransaction().commit();
}
// Change copy option and validate
public void testCopy() {
id++;
em.getTransaction().begin();
create(id);
em.persist(e1);
compat.setCopyOnDetach(true);
Entity1 e1Det = em.detach(e1);
assertNotEquals(e1, e1Det);
assertTrue(em.contains(e1));
em.getTransaction().commit();
compat.setCopyOnDetach(false);
}
// Change flush option and validate
public void testFlush() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
em.getTransaction().commit();
compat.setFlushBeforeDetach(true);
em.getTransaction().begin();
e1 = em.find(Entity1.class, id);
assertEquals("entity1",e1.getName());
e1.setName("new name");
em.clear(e1);
em.getTransaction().commit();
em.getTransaction().begin();
em.clear();
e1 = em.find(Entity1.class, id);
assertEquals("new name", e1.getName());
em.getTransaction().commit();
compat.setFlushBeforeDetach(false);
}
// Clear an entity, then recreate it in the same transaction.
public void testClearRecreate() {
id++;
em.getTransaction().begin();
create(id);
em.persistAll(allEntities);
em.clear(e1);
em.merge(e1);
em.getTransaction().commit();
em.getTransaction().begin();
em.clear();
Entity1 newE1 = em.find(Entity1.class, id);
assertNotNull(newE1);
em.getTransaction().commit();
}
}

View File

@ -46,7 +46,7 @@ public class TestDetach extends SingleEMFTestCase {
MetaDataRepository repos = emf.getConfiguration()
.getMetaDataRepositoryInstance();
ClassMetaData meta = repos.getCachedMetaData(DMCustomer.class);
assertEquals(ValueMetaData.CASCADE_IMMEDIATE, meta.getField("firstName").getCascadeDetach());
assertEquals(ValueMetaData.CASCADE_NONE, meta.getField("firstName").getCascadeDetach());
assertEquals(ValueMetaData.CASCADE_IMMEDIATE, meta.getField("customerInventories").getElement().getCascadeDetach());
meta = repos.getCachedMetaData(DMCustomerInventory.class);

View File

@ -41,7 +41,8 @@ public class TestNoCascadeOneToOneMerge
public void setUp() {
setUp(SimpleA.class, SimpleRef.class, SimpleB.class, SimpleC.class,
"openjpa.Compatibility", "FlushBeforeDetach=true,CopyOnDetach=true",
"openjpa.Compatibility", "FlushBeforeDetach=true," +
"CopyOnDetach=true",
CLEAR_TABLES);
createEntities();
}

View File

@ -37,7 +37,9 @@ public class TestLRS
private long id;
public void setUp() {
setUp(LRSEntity.class, BasicEntity.class, CLEAR_TABLES);
setUp(LRSEntity.class, BasicEntity.class, CLEAR_TABLES,
"openjpa.Compatibility", "default(copyOnDetach=true," +
"cascadeWithDetach=true)");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();