mirror of https://github.com/apache/openjpa.git
OPENJPA-885 Committing additional detach tests contributed by Dianne Richards
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@758442 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
66f829f7eb
commit
a4124b4743
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
* 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.Basic;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.apache.openjpa.persistence.FetchAttribute;
|
||||
import org.apache.openjpa.persistence.FetchGroup;
|
||||
import org.apache.openjpa.persistence.FetchGroups;
|
||||
|
||||
@Entity
|
||||
@Table(name="EntityA_detach")
|
||||
@FetchGroups({
|
||||
@FetchGroup(name = "loadD", attributes = {
|
||||
@FetchAttribute(name = "entityD", recursionDepth = 0)
|
||||
})
|
||||
})
|
||||
public class EntityA {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Basic(fetch=FetchType.LAZY)
|
||||
private String description;
|
||||
|
||||
@OneToOne(cascade=(CascadeType.ALL))
|
||||
EntityB entityB;
|
||||
|
||||
@OneToOne(cascade=(CascadeType.PERSIST))
|
||||
EntityC entityC;
|
||||
|
||||
@OneToOne(cascade=(CascadeType.ALL), fetch=FetchType.LAZY)
|
||||
EntityD entityD;
|
||||
|
||||
@OneToOne(cascade=(CascadeType.PERSIST), fetch=FetchType.LAZY)
|
||||
EntityE entityE;
|
||||
|
||||
public EntityA() {
|
||||
}
|
||||
|
||||
public EntityA(long id, String name, String description) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the description
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param description the description to set
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the entityB
|
||||
*/
|
||||
public EntityB getEntityB() {
|
||||
return entityB;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entityB the entityB to set
|
||||
*/
|
||||
public void setEntityB(EntityB entityB) {
|
||||
this.entityB = entityB;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the entityC
|
||||
*/
|
||||
public EntityC getEntityC() {
|
||||
return entityC;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entityC the entityC to set
|
||||
*/
|
||||
public void setEntityC(EntityC entityC) {
|
||||
this.entityC = entityC;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the entityD
|
||||
*/
|
||||
public EntityD getEntityD() {
|
||||
return entityD;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entityD the entityD to set
|
||||
*/
|
||||
public void setEntityD(EntityD entityD) {
|
||||
this.entityD = entityD;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the entityE
|
||||
*/
|
||||
public EntityE getEntityE() {
|
||||
return entityE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entityE the entityE to set
|
||||
*/
|
||||
public void setEntityE(EntityE entityE) {
|
||||
this.entityE = entityE;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="EntityB_detach")
|
||||
public class EntityB {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public EntityB() {
|
||||
}
|
||||
|
||||
public EntityB(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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="EntityC_detach")
|
||||
public class EntityC {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public EntityC() {
|
||||
}
|
||||
|
||||
public EntityC(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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="EntityD_detach")
|
||||
public class EntityD {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public EntityD() {
|
||||
}
|
||||
|
||||
public EntityD(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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="EntityE_detach")
|
||||
public class EntityE {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public EntityE() {
|
||||
}
|
||||
|
||||
public EntityE(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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,189 @@
|
|||
/*
|
||||
* 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 java.util.List;
|
||||
|
||||
import org.apache.openjpa.conf.Compatibility;
|
||||
import org.apache.openjpa.persistence.DetachStateType;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
/**
|
||||
* Test the various options for openjpa.DetachState with the new DETACH
|
||||
* cascade option.
|
||||
*
|
||||
* @author dianner
|
||||
* @since 2.0.0
|
||||
*
|
||||
*/
|
||||
public class TestDetachStateCascade extends SingleEMFTestCase {
|
||||
OpenJPAEntityManager em;
|
||||
int id = 0;
|
||||
Compatibility compat;
|
||||
|
||||
EntityA entityA;
|
||||
EntityB entityB;
|
||||
EntityC entityC;
|
||||
EntityD entityD;
|
||||
EntityE entityE;
|
||||
|
||||
List<EntityC> list = new ArrayList<EntityC>();
|
||||
|
||||
Collection<Object> allEntities = new HashSet<Object>();
|
||||
|
||||
public void setUp() throws Exception {
|
||||
setUp(org.apache.openjpa.persistence.detach.EntityA.class,
|
||||
org.apache.openjpa.persistence.detach.EntityB.class,
|
||||
org.apache.openjpa.persistence.detach.EntityC.class,
|
||||
org.apache.openjpa.persistence.detach.EntityD.class,
|
||||
org.apache.openjpa.persistence.detach.EntityE.class);
|
||||
assertNotNull(emf);
|
||||
em = emf.createEntityManager();
|
||||
assertNotNull(em);
|
||||
compat = emf.getConfiguration().getCompatibilityInstance();
|
||||
assertNotNull(compat);
|
||||
id++;
|
||||
create(id);
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.persist(entityA);
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
|
||||
private void create(int id) {
|
||||
entityA = new EntityA(id, "entityA", "top level class");
|
||||
entityB = new EntityB(id, "entityB");
|
||||
entityC = new EntityC(id, "entityC");
|
||||
entityD = new EntityD(id, "entityD");
|
||||
entityE = new EntityE(id, "entityE");
|
||||
|
||||
entityA.setEntityB(entityB);
|
||||
entityA.setEntityC(entityC);
|
||||
entityA.setEntityD(entityD);
|
||||
entityA.setEntityE(entityE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The default DetachState of LOADED is tested. In this scenario:
|
||||
* A is the main entity to be detached
|
||||
* B is loaded and DETACH cascade is specified
|
||||
* C is loaded but DETACH cascade is NOT specified
|
||||
* D is not loaded and DETACH cascade is specified
|
||||
* E is not loaded but DETACH cascade is NOT specified
|
||||
*/
|
||||
public void testLoaded() {
|
||||
em.getTransaction().begin();
|
||||
// Clear the persistence context so we start fresh
|
||||
em.persist(entityA);
|
||||
em.clear();
|
||||
EntityA eA = em.find(EntityA.class, id); // Loads B and C by default
|
||||
assertTrue(em.contains(eA));
|
||||
EntityD eD = em.find(EntityD.class, id); // Load independently
|
||||
assertTrue(em.contains(eD));
|
||||
EntityE eE = em.find(EntityE.class, id); // Load independently
|
||||
assertTrue(em.contains(eE));
|
||||
EntityA entityADet = em.detach(eA);
|
||||
assertEquals(id, entityADet.getId());
|
||||
assertEquals("entityA", entityADet.getName());
|
||||
assertNull(entityADet.getDescription()); // should not be loaded
|
||||
assertNotNull(entityADet.getEntityB());
|
||||
assertNotNull(entityADet.getEntityC());
|
||||
assertNull(entityADet.getEntityD());
|
||||
assertNull(entityADet.getEntityE());
|
||||
assertTrue(em.isDetached(eA));
|
||||
assertTrue(em.isDetached(entityADet.getEntityB()));
|
||||
assertFalse(em.isDetached(entityADet.getEntityC()));
|
||||
assertFalse(em.isDetached(eD));
|
||||
assertFalse(em.isDetached(eE));
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* The DetachState of ALL is tested. In this scenario:
|
||||
* A is the main entity to be detached.
|
||||
* B, C, D, and E are all loaded
|
||||
* DETACH cascade is specified for B and D
|
||||
* DETACH cascade is NOT specified for C and E
|
||||
*/
|
||||
public void testDetachStateAll() {
|
||||
em.setDetachState(DetachStateType.ALL);
|
||||
em.getTransaction().begin();
|
||||
// Clear the persistence context so we start fresh
|
||||
em.persist(entityA);
|
||||
em.clear();
|
||||
EntityA eA = em.find(EntityA.class, id);
|
||||
assertTrue(em.contains(eA));
|
||||
EntityA entityADet = em.detach(eA);
|
||||
assertEquals(id, entityADet.getId());
|
||||
assertEquals("entityA", entityADet.getName());
|
||||
assertNotNull(entityADet.getDescription());
|
||||
assertNotNull(entityADet.getEntityB());
|
||||
assertNotNull(entityADet.getEntityC());
|
||||
assertNotNull(entityADet.getEntityD());
|
||||
assertNotNull(entityADet.getEntityE());
|
||||
assertTrue(em.isDetached(eA));
|
||||
assertTrue(em.isDetached(entityADet.getEntityB()));
|
||||
assertFalse(em.isDetached(entityADet.getEntityC()));
|
||||
assertTrue(em.isDetached(entityADet.getEntityD()));
|
||||
assertFalse(em.isDetached(entityADet.getEntityE()));
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* The default DetachState of LOADED is tested. In this scenario:
|
||||
* A is the main entity to be detached
|
||||
* B is loaded by default and DETACH cascade is specified
|
||||
* C is loaded by default but DETACH cascade is NOT specified
|
||||
* D is loaded by fetch plan and DETACH cascade is specified
|
||||
* E is not loaded but DETACH cascade is NOT specified
|
||||
*/
|
||||
public void testDetachStateFetchGroup() {
|
||||
em.setDetachState(DetachStateType.FETCH_GROUPS);
|
||||
em.getFetchPlan().addFetchGroup("loadD");
|
||||
em.getTransaction().begin();
|
||||
// Clear the persistence context so we start fresh
|
||||
em.persist(entityA);
|
||||
em.clear();
|
||||
EntityA eA = em.find(EntityA.class, id);
|
||||
assertTrue(em.contains(eA));
|
||||
EntityE eE = em.find(EntityE.class, id); // Load independently
|
||||
assertTrue(em.contains(eE));
|
||||
EntityA entityADet = em.detach(eA);
|
||||
assertEquals(id, entityADet.getId());
|
||||
assertEquals("entityA", entityADet.getName());
|
||||
assertNull(entityADet.getDescription());
|
||||
assertNotNull(entityADet.getEntityB());
|
||||
assertNotNull(entityADet.getEntityC());
|
||||
assertNotNull(entityADet.getEntityD());
|
||||
assertNull(entityADet.getEntityE());
|
||||
assertTrue(em.isDetached(eA));
|
||||
assertTrue(em.isDetached(entityADet.getEntityB()));
|
||||
assertFalse(em.isDetached(entityADet.getEntityC()));
|
||||
assertTrue(em.isDetached(entityADet.getEntityD()));
|
||||
assertFalse(em.isDetached(eE));
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue