mirror of https://github.com/apache/openjpa.git
OPENJPA-1702: Allow new entities to be dirtied in BeforeCommit callback even if there are no other dirty entities. Based on patch submitted by Heath Thomann
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@963499 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6210611374
commit
f03b417d41
|
@ -3986,8 +3986,10 @@ public class BrokerImpl
|
||||||
* Return a copy of all transactional state managers.
|
* Return a copy of all transactional state managers.
|
||||||
*/
|
*/
|
||||||
protected Collection<StateManagerImpl> getTransactionalStates() {
|
protected Collection<StateManagerImpl> getTransactionalStates() {
|
||||||
if (!hasTransactionalObjects())
|
if (!hasTransactionalObjects()) {
|
||||||
return Collections.EMPTY_SET;
|
// return a new empty set. Entities may be added by TransactionListeners
|
||||||
|
return new LinkedHashSet<StateManagerImpl>();
|
||||||
|
}
|
||||||
return _transCache.copy();
|
return _transCache.copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4707,8 +4709,10 @@ public class BrokerImpl
|
||||||
* Return a copy of all transactional state managers.
|
* Return a copy of all transactional state managers.
|
||||||
*/
|
*/
|
||||||
public Collection copy() {
|
public Collection copy() {
|
||||||
if (isEmpty())
|
if (isEmpty()) {
|
||||||
return Collections.EMPTY_SET;
|
// Transaction Listeners may add entities to the transaction.
|
||||||
|
return new LinkedHashSet();
|
||||||
|
}
|
||||||
|
|
||||||
// size may not be entirely accurate due to refs expiring, so
|
// size may not be entirely accurate due to refs expiring, so
|
||||||
// manually copy each object; doesn't matter this way if size too
|
// manually copy each object; doesn't matter this way if size too
|
||||||
|
|
|
@ -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.event;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Version;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class AnEntity {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@Version
|
||||||
|
private int version;
|
||||||
|
|
||||||
|
public int getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(int version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
return "id = " + id + ", name = " + name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,140 @@
|
||||||
|
/*
|
||||||
|
* 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.event;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityTransaction;
|
||||||
|
import javax.persistence.Query;
|
||||||
|
|
||||||
|
import org.apache.openjpa.event.TransactionEvent;
|
||||||
|
import org.apache.openjpa.event.TransactionListener;
|
||||||
|
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
||||||
|
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
|
||||||
|
import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
|
||||||
|
|
||||||
|
public class TestBeforeCommit extends AbstractPersistenceTestCase implements TransactionListener {
|
||||||
|
|
||||||
|
AnEntity ae = null;
|
||||||
|
public final int PKID = 2;
|
||||||
|
|
||||||
|
private static OpenJPAEntityManagerFactorySPI emf = null;
|
||||||
|
|
||||||
|
public void setUp() {
|
||||||
|
if (emf == null) {
|
||||||
|
emf = createEMF(AnEntity.class);
|
||||||
|
}
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
EntityTransaction tran = em.getTransaction();
|
||||||
|
|
||||||
|
tran.begin();
|
||||||
|
em.createQuery("Delete from AnEntity").executeUpdate();
|
||||||
|
tran.commit();
|
||||||
|
|
||||||
|
tran.begin();
|
||||||
|
ae = new AnEntity();
|
||||||
|
ae.setId(PKID);
|
||||||
|
ae.setName("");
|
||||||
|
em.persist(ae);
|
||||||
|
tran.commit();
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testQuery() {
|
||||||
|
OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) emf.createEntityManager();
|
||||||
|
em.addTransactionListener(this);
|
||||||
|
EntityTransaction tran = em.getTransaction();
|
||||||
|
|
||||||
|
tran.begin();
|
||||||
|
ae = doQuery(em);
|
||||||
|
assertEquals("", ae.getName());
|
||||||
|
assertEquals(1, ae.getVersion());
|
||||||
|
tran.commit();
|
||||||
|
|
||||||
|
ae = doQuery(em);
|
||||||
|
assertEquals("Ava", ae.getName());
|
||||||
|
assertEquals(2, ae.getVersion());
|
||||||
|
|
||||||
|
em.clear();
|
||||||
|
ae = em.find(AnEntity.class, PKID);
|
||||||
|
assertEquals("Ava", ae.getName());
|
||||||
|
assertEquals(2, ae.getVersion());
|
||||||
|
|
||||||
|
tran.begin();
|
||||||
|
tran.commit();
|
||||||
|
em.clear();
|
||||||
|
ae = em.find(AnEntity.class, PKID);
|
||||||
|
assertEquals("AvaAva", ae.getName());
|
||||||
|
assertEquals(3, ae.getVersion());
|
||||||
|
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testEmptyTransaction() {
|
||||||
|
OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) emf.createEntityManager();
|
||||||
|
em.addTransactionListener(this);
|
||||||
|
EntityTransaction tran = em.getTransaction();
|
||||||
|
ae = doQuery(em);
|
||||||
|
assertEquals("", ae.getName());
|
||||||
|
assertEquals(1, ae.getVersion());
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
tran.begin();
|
||||||
|
tran.commit();
|
||||||
|
|
||||||
|
// when BeforeCommit was fired AE was not managed. As a result its state is out of sync with the database.
|
||||||
|
assertEquals("Ava", ae.getName());
|
||||||
|
ae = doQuery(em);
|
||||||
|
assertEquals("", ae.getName());
|
||||||
|
assertEquals(1, ae.getVersion());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void beforeCommit(TransactionEvent event) {
|
||||||
|
ae.setName(ae.getName() + "Ava");
|
||||||
|
}
|
||||||
|
|
||||||
|
private AnEntity doQuery(EntityManager em) {
|
||||||
|
Query q = em.createQuery("select a from AnEntity a where a.id = :id");
|
||||||
|
return (AnEntity) q.setParameter("id", PKID).getSingleResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unused Interface methods
|
||||||
|
public void afterBegin(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void afterFlush(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void beforeFlush(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void afterCommit(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void afterCommitComplete(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void afterRollback(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void afterRollbackComplete(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void afterStateTransitions(TransactionEvent event) {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue