Author: hthomann
Date: Thu Oct 20 16:29:48 2011
New Revision: 1186889
URL: http://svn.apache.org/viewvc?rev=1186889&view=rev
Log:
OPENJPA-2051: Change to ensure entities are properly cascaded after a flush.
Added:
openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Edge.java
(with props)
openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestMultiCascadePersist.java
(with props)
openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Vertex.java
(with props)
openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/VertexType.java
(with props)
Modified:
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Modified: openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java?rev=1186889&r1=1186888&r2=1186889&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
(original)
+++ openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
Thu Oct 20 16:29:48 2011
@@ -69,6 +69,8 @@ public class Compatibility {
private boolean _convertPositionalParametersToNamed = false;
private boolean _checkDatabaseForCascadePersistToDetachedEntity = true;
private boolean _parseAnnotationsForQueryMode = true;
+ private boolean _resetFlushFlagForCascadePersist = false;//OPENJPA-2051
+
/**
* Whether to require exact identity value types when creating object
* ids from a class and value. Defaults to false.
@@ -588,5 +590,33 @@ public class Compatibility {
public void setParseAnnotationsForQueryMode(boolean parseAnnotationsForQueryMode) {
_parseAnnotationsForQueryMode = parseAnnotationsForQueryMode;
}
+
+ /**
+ * Whether OpenJPA should reset the internal state (flush flag) when cascading a persist
to another
+ * Entity. That is, when a flush is performed, OpenJPA keep state to indicate the flush
has been
+ * performed. In certain cascade persist scenarios the fact that a flush has been performed
prior to
+ * a cascade persist can cause certain entities to not be written to the database given
the prior
+ * flush. This property, when set, will cause the flush flag to be reset in cascade
scenarios. For more
+ * details see JIRA OPENJPA-2051
+ *
+ * @since 2.0.x
+ */
+ public boolean getResetFlushFlagForCascadePersist(){
+ return _resetFlushFlagForCascadePersist;
+ }
+
+ /**
+ * Whether OpenJPA should reset the internal state (flush flag) when cascading a persist
to another
+ * Entity. That is, when a flush is performed, OpenJPA keep state to indicate the flush
has been
+ * performed. In certain cascade persist scenarios the fact that a flush has been performed
prior to
+ * a cascade persist can cause certain entities to not be written to the database given
the prior
+ * flush. This property, when set, will cause the flush flag to be reset in cascade
scenarios. For more
+ * details see JIRA OPENJPA-2051
+ *
+ * @since 2.0.x
+ */
+ public void setResetFlushFlagForCascadePersist(boolean b){
+ _resetFlushFlagForCascadePersist = b;
+ }
}
Modified: openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1186889&r1=1186888&r2=1186889&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
(original)
+++ openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Thu Oct 20 16:29:48 2011
@@ -4083,7 +4083,10 @@ public class BrokerImpl
lock();
try {
switch (status) {
- case STATUS_INIT:
+ case STATUS_INIT:
+ if (_compat.getResetFlushFlagForCascadePersist()){//OPENJPA-2051
+ _flags &= ~FLAG_FLUSHED;
+ }
_cache.add(sm);
break;
case STATUS_TRANSIENT:
Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Edge.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Edge.java?rev=1186889&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Edge.java
(added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Edge.java
Thu Oct 20 16:29:48 2011
@@ -0,0 +1,59 @@
+/*
+ * 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;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+
+@Entity
+public class Edge {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private long oid;
+
+ @ManyToOne(cascade = CascadeType.ALL)
+ @JoinColumn(name = "SOURCE_OID")
+ private Vertex source;
+
+ @ManyToOne(cascade = CascadeType.ALL)
+ @JoinColumn(name = "TARGET_OID")
+ private Vertex target;
+
+ protected Edge() {
+ }
+
+ Edge( Vertex src ) {
+ this();
+ this.source = src;
+ }
+
+ public void setTarget( Vertex node ) {
+ this.target = node;
+ }
+
+ public long getOid() {
+ return oid;
+ }
+}
Propchange: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Edge.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestMultiCascadePersist.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestMultiCascadePersist.java?rev=1186889&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestMultiCascadePersist.java
(added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestMultiCascadePersist.java
Thu Oct 20 16:29:48 2011
@@ -0,0 +1,117 @@
+/*
+ * 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;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.NoResultException;
+import javax.persistence.TypedQuery;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestMultiCascadePersist extends SingleEMFTestCase {
+
+ @Override
+ public void setUp() throws Exception {
+ setUp(DROP_TABLES, Vertex.class, VertexType.class, Edge.class);
+ }
+
+ public void testSingleTransaction() {
+ OpenJPAEntityManager em = emf.createEntityManager();
+ EntityTransaction tx = em.getTransaction();
+
+ tx.begin();
+
+ //The flush is important to the rest of the test. If this
+ //is removed, the test works as expected. While the flush
+ //at this point in the test may seem odd/unnecessary, it
+ //is more clear to perform a flush directly rather than
+ //something (e.g. query) which would cause a flush under
+ //the covers. See OPENJPA-2051 for more details.
+ em.flush();
+
+ VertexType defaultType = new VertexType( "default" );
+ VertexType specialType = new VertexType( "special" );
+
+ em.persist(defaultType);
+ em.persist(specialType);
+
+ Vertex src = new Vertex( defaultType );
+ Vertex target = new Vertex( specialType );
+
+ Edge t = src.newEdge( target );
+ assertNotNull( t );
+
+ em.persist(src);
+
+ tx.commit();
+
+ TypedQuery<Edge> q = em.createQuery( "SELECT t FROM Edge t", Edge.class );
+ List<Edge> resultList = q.getResultList();
+
+ assertEquals( 1, resultList.size() );
+ assertEquals( 2, findAllVertexType(em).size() );
+ if (emf.getConfiguration().getCompatibilityInstance().getResetFlushFlagForCascadePersist()){
+ assertEquals( 2, findAllVertex(em).size() );
+ }
+ else{
+ //There *should* be 2 Vertex....but by default we can not fix this without a
+ //compatibility flag.
+ assertEquals( 1, findAllVertex(em).size() );
+ }
+ }
+
+ public VertexType findVertexTypeByName(EntityManager em, String name ) {
+ try {
+ TypedQuery<VertexType> query = em.createNamedQuery( "VertexType.findByName",
+ VertexType.class );
+ query.setParameter( 1, name );
+ return query.getSingleResult();
+ } catch ( NoResultException nre ) {
+ return null;
+ }
+ }
+
+ public List<VertexType> findAllVertexType(EntityManager em) {
+ try {
+ TypedQuery<VertexType> query = em.createNamedQuery( "VertexType.findAll",
+ VertexType.class );
+ return query.getResultList();
+ } catch ( NoResultException nre ) {
+ return Collections.emptyList();
+ }
+ }
+
+ public List<Vertex> findAllVertex(EntityManager em) {
+ try {
+ TypedQuery<Vertex> query = em.createNamedQuery( "Vertex.findAll",
+ Vertex.class );
+ return query.getResultList();
+ } catch ( NoResultException nre ) {
+ return Collections.emptyList();
+ }
+ }
+}
+
+
+
Propchange: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestMultiCascadePersist.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Vertex.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Vertex.java?rev=1186889&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Vertex.java
(added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Vertex.java
Thu Oct 20 16:29:48 2011
@@ -0,0 +1,78 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
+
+
+@Entity
+@NamedQueries({
+ @NamedQuery(name = "Vertex.findByName",
+ query = "SELECT n FROM Vertex n where n.type.name=?1"),
+ @NamedQuery(name = "Vertex.findAll", query = "SELECT n FROM Vertex n") })
+public class Vertex {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private long oid;
+
+ @OneToMany(mappedBy = "source", cascade = CascadeType.ALL)
+ private List<Edge> outgoing;
+
+ @OneToMany(mappedBy = "target", cascade = CascadeType.ALL)
+ private List<Edge> incoming;
+
+ @ManyToOne(cascade = CascadeType.ALL)
+ @JoinColumn(name = "TYPE_OID")
+ private VertexType type;
+
+ protected Vertex() {
+ this.incoming = new ArrayList<Edge>();
+ this.outgoing = new ArrayList<Edge>();
+ }
+
+ public Vertex( VertexType type ) {
+ this();
+ this.type = type;
+ type.instances.add( this );
+ }
+
+ public Edge newEdge( Vertex target ) {
+ Edge t = new Edge( this );
+ outgoing.add( t );
+ t.setTarget( target );
+ return t;
+ }
+
+ public long getOid() {
+ return oid;
+ }
+}
Propchange: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/Vertex.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/VertexType.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/VertexType.java?rev=1186889&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/VertexType.java
(added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/VertexType.java
Thu Oct 20 16:29:48 2011
@@ -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.cascade;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
+
+@Entity
+@NamedQueries({
+ @NamedQuery(name = "VertexType.findByName",
+ query = "SELECT t FROM VertexType t where t.name=?1"),
+ @NamedQuery(name = "VertexType.findAll",
+ query = "SELECT t FROM VertexType t") })
+public class VertexType {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private long oid;
+
+ @OneToMany(mappedBy = "type", cascade = CascadeType.ALL)
+ List<Vertex> instances;
+
+ private String name;
+
+ protected VertexType() {
+ this.instances = new ArrayList<Vertex>();
+ }
+
+ public VertexType( String name ) {
+ this();
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public long getOid() {
+ return oid;
+ }
+}
Propchange: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/VertexType.java
------------------------------------------------------------------------------
svn:eol-style = native
|