OPENJPA-1988: openjpa does not process persistence unit default <cascade-persist>

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.3.x@1636446 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jody Grassel 2014-11-03 21:22:20 +00:00
parent 64c1eb9273
commit d07e3828c6
20 changed files with 1456 additions and 5 deletions

View File

@ -63,6 +63,8 @@ import org.apache.openjpa.util.ObjectId;
public class FieldMapping
extends FieldMetaData
implements ValueMapping, FieldStrategy {
private static final long serialVersionUID = 142185362294762433L;
private static final Localizer _loc = Localizer.forPackage
(FieldMapping.class);

View File

@ -50,6 +50,8 @@ import org.apache.openjpa.util.MetaDataException;
public class ValueMappingImpl
extends ValueMetaDataImpl
implements ValueMapping {
private static final long serialVersionUID = 6440545965133775709L;
private static final Localizer _loc = Localizer.forPackage
(ValueMappingImpl.class);

View File

@ -78,6 +78,8 @@ public class FieldMetaData
extends Extensions
implements ValueMetaData, MetaDataContext, MetaDataModes, Commentable {
private static final long serialVersionUID = -566180883009883198L;
/**
* Constant specifying that no null-value was given.
*/
@ -2190,6 +2192,10 @@ public class FieldMetaData
public void setCascadePersist(int persist) {
_val.setCascadePersist(persist);
}
public void setCascadePersist(int cascade, boolean checkPUDefault) {
_val.setCascadePersist(cascade, checkPUDefault);
}
public int getCascadeAttach() {
return _val.getCascadeAttach();

View File

@ -126,5 +126,13 @@ public interface MetaDataDefaults
* @since 2.0.0
*/
public boolean isNonDefaultMappingAllowed(OpenJPAConfiguration conf);
/**
* Whether cascade-persist was declared in the persistence unit defaults.
* @return
*/
public Boolean isDefaultCascadePersistEnabled();
public void setDefaultCascadePersistEnabled(Boolean bool);
}

View File

@ -161,6 +161,14 @@ public class NoneMetaDataFactory
public Class<?> getManagedClass(Class<?> c) {
return null;
}
public Boolean isDefaultCascadePersistEnabled() {
return false;
}
public void setDefaultCascadePersistEnabled(Boolean bool) {
}
public boolean isAbstractMappingUniDirectional(OpenJPAConfiguration conf) {
return false;

View File

@ -203,6 +203,13 @@ public interface ValueMetaData
*/
public void setCascadePersist(int cascade);
/**
* Cascade behavior for persist operation.
*
* @see #getCascadePersist
*/
public void setCascadePersist(int cascade, boolean checkPUDefault);
/**
* Cascade behavior for attach operation. Only applies to
* persistence-capable values. Options are:<br />

View File

@ -30,6 +30,8 @@ import org.apache.openjpa.util.UserException;
*/
public class ValueMetaDataImpl
implements ValueMetaData {
private static final long serialVersionUID = 6766697443293395831L;
private static final Localizer _loc = Localizer.forPackage
(ValueMetaDataImpl.class);
@ -58,6 +60,7 @@ public class ValueMetaDataImpl
private int _resMode = MODE_NONE;
private String _mappedBy = null;
private FieldMetaData _mappedByMeta = null;
private boolean _checkPUDefaultCascadePersist = true;
protected ValueMetaDataImpl(FieldMetaData owner) {
_owner = owner;
@ -236,15 +239,52 @@ public class ValueMetaDataImpl
if (_owner.getManagement() != FieldMetaData.MANAGE_PERSISTENT)
return CASCADE_NONE;
if (isDeclaredTypePC())
return _persist;
return checkPUDefaultCascadePersist();
if (!isTypePC())
return CASCADE_NONE;
// if only externalized type is pc, can't cascade immediate
return (_persist == CASCADE_IMMEDIATE) ? CASCADE_AUTO : _persist;
return (_persist == CASCADE_IMMEDIATE) ? CASCADE_AUTO : checkPUDefaultCascadePersist();
}
/**
* Check if the persistence unit default <cascade-persist> has been enabled. If so, then change
* CASCADE_NONE to CASCADE_IMMEDIATE.
* @return
*/
private int checkPUDefaultCascadePersist() {
if (_checkPUDefaultCascadePersist) {
// Apply default <cascade-persist> only to entity relationships
boolean applyDefaultCascadePersist = false;
switch (_owner.getAssociationType()) {
case FieldMetaData.ONE_TO_ONE:
case FieldMetaData.ONE_TO_MANY:
case FieldMetaData.MANY_TO_MANY:
case FieldMetaData.MANY_TO_ONE:
applyDefaultCascadePersist = true;
default:
}
if (applyDefaultCascadePersist) {
Boolean dcpe = getRepository().getMetaDataFactory().getDefaults().isDefaultCascadePersistEnabled();
if (dcpe != null && dcpe.equals(Boolean.TRUE) && _persist == CASCADE_NONE) {
_persist = CASCADE_IMMEDIATE;
}
}
_checkPUDefaultCascadePersist = false;
}
return _persist;
}
public void setCascadePersist(int persist) {
setCascadePersist(persist, true);
}
public void setCascadePersist(int persist, boolean checkPUDefault) {
_persist = persist;
_checkPUDefaultCascadePersist = checkPUDefault;
}
public int getCascadeAttach() {

View File

@ -0,0 +1,47 @@
/*
* 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.cascade.pudefault;
import javax.persistence.Basic;
import javax.persistence.Embeddable;
@Embeddable
public class AnEmbeddable {
@Basic
private String eStrData;
public AnEmbeddable() {
}
public String geteStrData() {
return eStrData;
}
public void seteStrData(String eStrData) {
this.eStrData = eStrData;
}
@Override
public String toString() {
return "AnEmbeddable [eStrData=" + eStrData + "]";
}
}

View File

@ -0,0 +1,88 @@
/*
* 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.cascade.pudefault;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Embeddable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Embeddable
public class EmbeddableWithRelationships {
@ManyToMany
private Collection<PUDEntityB> colM2M;
@OneToMany
private Collection<PUDEntityB> colO2M;
@ManyToOne
private PUDEntityB m2o;
@OneToOne
private PUDEntityB o2o;
public EmbeddableWithRelationships() {
colM2M = new ArrayList<PUDEntityB>();
colO2M = new ArrayList<PUDEntityB>();
}
public Collection<PUDEntityB> getColM2M() {
return colM2M;
}
public void setColM2M(Collection<PUDEntityB> colM2M) {
this.colM2M = colM2M;
}
public Collection<PUDEntityB> getColO2M() {
return colO2M;
}
public void setColO2M(Collection<PUDEntityB> colO2M) {
this.colO2M = colO2M;
}
public PUDEntityB getM2o() {
return m2o;
}
public void setM2o(PUDEntityB m2o) {
this.m2o = m2o;
}
public PUDEntityB getO2o() {
return o2o;
}
public void setO2o(PUDEntityB o2o) {
this.o2o = o2o;
}
@Override
public String toString() {
return "EmbeddableWithRelationships [colM2M=" + colM2M + ", colO2M="
+ colO2M + ", m2o=" + m2o + ", o2o=" + o2o + "]";
}
}

View File

@ -0,0 +1,116 @@
/*
* 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.cascade.pudefault;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class PUDEntityA01 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String strData;
@ManyToMany
private Collection<PUDEntityB> colM2M;
@OneToMany
private Collection<PUDEntityB> colO2M;
@ManyToOne
private PUDEntityB m2o;
@OneToOne
private PUDEntityB o2o;
public PUDEntityA01() {
colM2M = new ArrayList<PUDEntityB>();
colO2M = new ArrayList<PUDEntityB>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStrData() {
return strData;
}
public void setStrData(String strData) {
this.strData = strData;
}
public Collection<PUDEntityB> getColM2M() {
return colM2M;
}
public void setColM2M(Collection<PUDEntityB> colM2M) {
this.colM2M = colM2M;
}
public Collection<PUDEntityB> getColO2M() {
return colO2M;
}
public void setColO2M(Collection<PUDEntityB> colO2M) {
this.colO2M = colO2M;
}
public PUDEntityB getM2o() {
return m2o;
}
public void setM2o(PUDEntityB m2o) {
this.m2o = m2o;
}
public PUDEntityB getO2o() {
return o2o;
}
public void setO2o(PUDEntityB o2o) {
this.o2o = o2o;
}
@Override
public String toString() {
return "PUDEntityA01 [id=" + id + ", strData=" + strData + ", colM2M="
+ colM2M + ", colO2M=" + colO2M + ", m2o=" + m2o + ", o2o=" + o2o
+ "]";
}
}

View File

@ -0,0 +1,75 @@
/*
* 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.cascade.pudefault;
import javax.persistence.Basic;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PUDEntityA02 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String strData;
@Embedded
private EmbeddableWithRelationships emb;
public PUDEntityA02() {
emb = new EmbeddableWithRelationships();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStrData() {
return strData;
}
public void setStrData(String strData) {
this.strData = strData;
}
public EmbeddableWithRelationships getEmb() {
return emb;
}
public void setEmb(EmbeddableWithRelationships emb) {
this.emb = emb;
}
@Override
public String toString() {
return "PUDEntityA02 [id=" + id + ", strData=" + strData + ", emb="
+ emb + "]";
}
}

View File

@ -0,0 +1,128 @@
/*
* 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.cascade.pudefault;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class PUDEntityAE01 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String strData;
@ManyToMany
private Collection<PUDEntityB> colM2M;
@OneToMany
private Collection<PUDEntityB> colO2M;
@ManyToOne
private PUDEntityB m2o;
@OneToOne
private PUDEntityB o2o;
@Embedded
private AnEmbeddable ane;
public PUDEntityAE01() {
colM2M = new ArrayList<PUDEntityB>();
colO2M = new ArrayList<PUDEntityB>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStrData() {
return strData;
}
public void setStrData(String strData) {
this.strData = strData;
}
public Collection<PUDEntityB> getColM2M() {
return colM2M;
}
public void setColM2M(Collection<PUDEntityB> colM2M) {
this.colM2M = colM2M;
}
public Collection<PUDEntityB> getColO2M() {
return colO2M;
}
public void setColO2M(Collection<PUDEntityB> colO2M) {
this.colO2M = colO2M;
}
public PUDEntityB getM2o() {
return m2o;
}
public void setM2o(PUDEntityB m2o) {
this.m2o = m2o;
}
public PUDEntityB getO2o() {
return o2o;
}
public void setO2o(PUDEntityB o2o) {
this.o2o = o2o;
}
public AnEmbeddable getAne() {
return ane;
}
public void setAne(AnEmbeddable ane) {
this.ane = ane;
}
@Override
public String toString() {
return "PUDEntityAE01 [id=" + id + ", strData=" + strData + ", colM2M="
+ colM2M + ", colO2M=" + colO2M + ", m2o=" + m2o + ", o2o=" + o2o
+ ", ane=" + ane + "]";
}
}

View File

@ -0,0 +1,62 @@
/*
* 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.cascade.pudefault;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PUDEntityB {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String strData;
public PUDEntityB() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStrData() {
return strData;
}
public void setStrData(String strData) {
this.strData = strData;
}
@Override
public String toString() {
return "PUDEntityB [id=" + id + ", strData=" + strData + "]";
}
}

View File

@ -0,0 +1,271 @@
/*
* 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.cascade.pudefault;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestNoPUDefaultCascadePersist extends SingleEMFTestCase {
public void setUp() throws Exception {
super.setUp(PUDEntityA01.class, PUDEntityAE01.class, PUDEntityB.class, AnEmbeddable.class,
CLEAR_TABLES);
}
public void testPUDefaultCascadePersistOverM2M() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColM2M().add(b);
}
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverO2M() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColO2M().add(b);
}
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverO2O() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setO2o(b);
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverM2O() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setM2o(b);
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverM2MWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColM2M().add(b);
}
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverO2MWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColO2M().add(b);
}
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverO2OWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setO2o(b);
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
public void testPUDefaultCascadePersistOverM2OWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setM2o(b);
em.persist(entity);
try {
em.getTransaction().commit();
fail("No Exception thrown.");
} catch (Exception e) {
// Expected
}
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
}

View File

@ -0,0 +1,504 @@
/*
* 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.cascade.pudefault;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestPUDefaultCascadePersist extends SingleEMFTestCase {
private EntityManagerFactory emf = null;
public void setUp() throws Exception {
super.setUp();
emf = OpenJPAPersistence.
createEntityManagerFactory("TestPUDefaultCascadePersist",
"org/apache/openjpa/persistence/cascade/pudefault/META-INF/persistence.xml");
// super.setUp(PUDEntityA01.class, PUDEntityB.class,
// "org/apache/openjpa/persistence/cascade/pudefault/META-INF/cascadepersistorm.xml",
// CLEAR_TABLES);
}
public void tearDown() throws Exception {
super.tearDown();
emf.close();
}
public void testPUDefaultCascadePersistOverM2M() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColM2M().add(b);
}
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA01 f_entity = em.find(PUDEntityA01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertEquals(10, f_entity.getColM2M().size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverO2M() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColO2M().add(b);
}
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA01 f_entity = em.find(PUDEntityA01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertEquals(10, f_entity.getColO2M().size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverO2O() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setO2o(b);
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA01 f_entity = em.find(PUDEntityA01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertNotNull(entity.getO2o());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverM2O() {
EntityManager em = emf.createEntityManager();
PUDEntityA01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA01();
entity.setStrData("PUDEntityA01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setM2o(b);
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA01 f_entity = em.find(PUDEntityA01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertNotNull(entity.getM2o());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverM2MWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColM2M().add(b);
}
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityAE01 f_entity = em.find(PUDEntityAE01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertEquals(10, f_entity.getColM2M().size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverO2MWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getColO2M().add(b);
}
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityAE01 f_entity = em.find(PUDEntityAE01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertEquals(10, f_entity.getColO2M().size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverO2OWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setO2o(b);
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityAE01 f_entity = em.find(PUDEntityAE01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertNotNull(entity.getO2o());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverM2OWithEmbed() {
EntityManager em = emf.createEntityManager();
PUDEntityAE01 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityAE01();
entity.setStrData("PUDEntityAE01");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.setM2o(b);
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityAE01 f_entity = em.find(PUDEntityAE01.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertNotNull(entity.getM2o());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverM2MEmbbedRel() {
EntityManager em = emf.createEntityManager();
PUDEntityA02 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA02();
entity.setStrData("PUDEntityA02");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getEmb().getColM2M().add(b);
}
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA02 f_entity = em.find(PUDEntityA02.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertEquals(10, f_entity.getEmb().getColM2M().size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverO2MEmbbedRel() {
EntityManager em = emf.createEntityManager();
PUDEntityA02 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA02();
entity.setStrData("PUDEntityA02");
for (int i = 0; i < 10; i++) {
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getEmb().getColO2M().add(b);
}
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA02 f_entity = em.find(PUDEntityA02.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertEquals(10, f_entity.getEmb().getColO2M().size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverO2OEmbbedRel() {
EntityManager em = emf.createEntityManager();
PUDEntityA02 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA02();
entity.setStrData("PUDEntityA02");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getEmb().setO2o(b);
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA02 f_entity = em.find(PUDEntityA02.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertNotNull(entity.getEmb().getO2o());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
public void testPUDefaultCascadePersistOverM2OEmbbedRel() {
EntityManager em = emf.createEntityManager();
PUDEntityA02 entity = null;
try {
em.getTransaction().begin();
entity = new PUDEntityA02();
entity.setStrData("PUDEntityA02");
PUDEntityB b = new PUDEntityB();
b.setStrData("B");
entity.getEmb().setM2o(b);
em.persist(entity);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
em.close();
em = emf.createEntityManager();
try {
PUDEntityA02 f_entity = em.find(PUDEntityA02.class, entity.getId());
assertNotNull(f_entity);
assertNotSame(entity, f_entity);
assertNotNull(entity.getEmb().getM2o());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
version="1.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<cascade-persist/>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>

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.
-->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="TestPUDefaultCascadePersist" transaction-type="RESOURCE_LOCAL">
<!-- <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> -->
<mapping-file>org/apache/openjpa/persistence/cascade/pudefault/META-INF/cascadepersistorm.xml</mapping-file>
<class>org.apache.openjpa.persistence.cascade.pudefault.PUDEntityA01</class>
<class>org.apache.openjpa.persistence.cascade.pudefault.PUDEntityAE01</class>
<class>org.apache.openjpa.persistence.cascade.pudefault.PUDEntityA02</class>
<class>org.apache.openjpa.persistence.cascade.pudefault.PUDEntityB</class>
<class>org.apache.openjpa.persistence.cascade.pudefault.AnEmbeddable</class>
<class>org.apache.openjpa.persistence.cascade.pudefault.EmbeddableWithRelationships</class>
<properties>
<property name="openjpa.ConnectionFactoryProperties" value="MaxActive=110, MaxIdle=10, ValidationTimeout=50000,
MaxCachedStatements=10, ValidationSQL='', MaxWait=10000, TestOnBorrow=true" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>

View File

@ -1770,7 +1770,7 @@ public class AnnotationPersistenceMetaDataParser
if (cascade == CascadeType.ALL || cascade == CascadeType.REMOVE)
vmd.setCascadeDelete(ValueMetaData.CASCADE_IMMEDIATE);
if (cascade == CascadeType.ALL || cascade == CascadeType.PERSIST)
vmd.setCascadePersist(ValueMetaData.CASCADE_IMMEDIATE);
vmd.setCascadePersist(ValueMetaData.CASCADE_IMMEDIATE, false);
if (cascade == CascadeType.ALL || cascade == CascadeType.MERGE)
vmd.setCascadeAttach(ValueMetaData.CASCADE_IMMEDIATE);
if (cascade == CascadeType.ALL || cascade == CascadeType.DETACH)

View File

@ -133,6 +133,7 @@ public class PersistenceMetaDataDefaults
protected SetterFilter setterFilter = new SetterFilter();
private Boolean _isAbstractMappingUniDirectional = null;
private Boolean _isNonDefaultMappingAllowed = null;
private Boolean _isCascadePersistPersistenceUnitDefaultEnabled = null;
public PersistenceMetaDataDefaults() {
setCallbackMode(CALLBACK_RETHROW | CALLBACK_ROLLBACK |
@ -945,4 +946,13 @@ public class PersistenceMetaDataDefaults
_isNonDefaultMappingAllowed = conf.getCompatibilityInstance().
isNonDefaultMappingAllowed();
}
public Boolean isDefaultCascadePersistEnabled() {
return _isCascadePersistPersistenceUnitDefaultEnabled;
}
public void setDefaultCascadePersistEnabled(Boolean bool) {
_isCascadePersistPersistenceUnitDefaultEnabled = bool;
}
}

View File

@ -63,6 +63,7 @@ import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.meta.LifecycleMetaData;
import org.apache.openjpa.meta.MetaDataContext;
import org.apache.openjpa.meta.MetaDataDefaults;
import org.apache.openjpa.meta.MetaDataFactory;
import org.apache.openjpa.meta.UpdateStrategies;
@ -1329,6 +1330,8 @@ public class XMLPersistenceMetaDataParser
throws SAXException {
if (!isMetaDataMode())
return false;
boolean puDefault = false;
Set<CascadeType> cascades = null;
if (currentElement() instanceof FieldMetaData) {
@ -1339,10 +1342,17 @@ public class XMLPersistenceMetaDataParser
if (_pkgCascades == null)
_pkgCascades = EnumSet.noneOf(CascadeType.class);
cascades = _pkgCascades;
puDefault = true;
}
boolean all = ELEM_CASCADE_ALL == tag;
if (all || ELEM_CASCADE_PER == tag)
if (all || ELEM_CASCADE_PER == tag) {
cascades.add(PERSIST);
if (puDefault) {
MetaDataDefaults mdd = _repos.getMetaDataFactory().getDefaults();
mdd.setDefaultCascadePersistEnabled(true);
}
}
if (all || ELEM_CASCADE_REM == tag)
cascades.add(REMOVE);
if (all || ELEM_CASCADE_MER == tag)
@ -1371,7 +1381,7 @@ public class XMLPersistenceMetaDataParser
for (CascadeType cascade : cascades) {
switch (cascade) {
case PERSIST:
vmd.setCascadePersist(ValueMetaData.CASCADE_IMMEDIATE);
vmd.setCascadePersist(ValueMetaData.CASCADE_IMMEDIATE, false);
break;
case MERGE:
vmd.setCascadeAttach(ValueMetaData.CASCADE_IMMEDIATE);