OPENJPA-722 resolution.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@700158 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
A. Abram White 2008-09-29 16:07:01 +00:00
parent baa01d79dd
commit 0226888590
4 changed files with 152 additions and 5 deletions

View File

@ -3167,8 +3167,8 @@ public class BrokerImpl
beginOperation(true);
try {
// If a flush is desired (based on input parm), then check if the
// "dirty" flag is set before calling flush().
if ((flush) && ((_flags & FLAG_FLUSH_REQUIRED) != 0))
// "dirty" flag is set before calling flush().
if (flush && (_flags & FLAG_FLUSH_REQUIRED) != 0)
flush();
detachAllInternal(call);
} catch (OpenJPAException ke) {

View File

@ -446,7 +446,8 @@ public class DetachManager
// detach fields and set detached variables
DetachedStateManager detSM = null;
if (_opts.getDetachedStateManager()
&& useDetachedStateManager(sm, _opts))
&& useDetachedStateManager(sm, _opts)
&& !(sm.isNew() && !sm.isDeleted() && !sm.isFlushed()))
detSM = new DetachedStateManager(detachedPC, sm, fields,
_opts.getAccessUnloaded(), _broker.getMultithreaded());
if (_full) {

View File

@ -79,7 +79,10 @@ public class DetachedStateManager
_embedded = sm.isEmbedded();
_loaded = load;
_access = access;
_dirty = new BitSet(_loaded.length());
if (!sm.isFlushed())
_dirty = (BitSet) sm.getDirty().clone();
else
_dirty = new BitSet(_loaded.length());
_oid = sm.fetchObjectId();
_version = sm.getVersion();
if (multithreaded)

View File

@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.simple;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
import java.math.BigDecimal;
/**
* Test case to ensure that the proper JPA clear semantics are processed.
@ -30,7 +31,7 @@ public class TestEntityManagerClear
extends SingleEMTestCase {
public void setUp() {
setUp(AllFieldTypes.class);
setUp(AllFieldTypes.class, Item.class,CLEAR_TABLES);
}
public void testDetach() {
@ -94,6 +95,148 @@ public class TestEntityManagerClear
rollback();
}
public void testNewClearMerge() {
// Create EntityManager and Start a transaction (1)
begin();
// Insert a new object then clear persistent context
AllFieldTypes testObject1 = new AllFieldTypes();
testObject1.setStringField("my test object1");
persist(testObject1);
//Object1 is not flushed to DB but only detached by clear().
em.clear();
assertEquals(0, query("select x from AllFieldTypes x "
+ "where x.stringField = 'my test object1'").
getResultList().size());
em.merge(testObject1);
commit();
//Start a new transaction
begin();
// Expect Object1 is persisted after merge and commit.
assertEquals(1, query("select x from AllFieldTypes x "
+ "where x.stringField = 'my test object1'").
getResultList().size());
// Rollback the transaction and close everything
rollback();
}
public void testUpdateClearMerge() {
// Create EntityManager and Start a transaction (1)
begin();
// Insert a new object
Item i = new Item();
i.setItemName("cup");
persist(i);
commit();
int id = i.getItemId();
begin();
Item i2 = em.find(Item.class, id);
i2.setItemName("fancy cup");
//Updated item is not flushed to DB but only detached by clear().
em.clear();
assertEquals(0, query("select x from Item x "
+ "where x.itemName = 'fancy cup'").
getResultList().size());
em.merge(i2);
commit();
//Start a new transaction
begin();
//should be changed by previous commit
assertEquals(1, query("select x from Item x "
+ "where x.itemName = 'fancy cup'").
getResultList().size());
// Rollback the transaction and close everything
rollback();
}
/**
* Test modify entity then clear context which cause unflushed modified entity detached.
* Do more modification on detached entity, merge back and commit.
* Expect both changes before clear and after clear are persisted.
*
*/
public void testUpdateClearUpdateMerge() {
// Create EntityManager and Start a transaction (1)
begin();
// Insert a new object
Item i = new Item();
i.setItemName("cup");
i.setItemPrice(new BigDecimal(100.00));
persist(i);
commit();
int id = i.getItemId();
begin();
Item i2 = em.find(Item.class, id);
i2.setItemName("fancy cup");
//Updated item is not flushed to DB but only detached by clear().
em.clear();
assertEquals(0, query("select x from Item x "
+ "where x.itemName = 'fancy cup'").
getResultList().size());
i2.setItemPrice(new BigDecimal(120.00));
em.merge(i2);
commit();
//Start a new transaction
begin();
//should be changed by previous commit
assertEquals(1, query("select x from Item x "
+ "where x.itemName = 'fancy cup' and x.itemPrice = 120.00").
getResultList().size());
// Rollback the transaction and close everything
rollback();
}
public void testUpdateFlushClearUpdateMerge() {
// Create EntityManager and Start a transaction (1)
begin();
// Insert a new object
Item i = new Item();
i.setItemName("cup");
i.setItemPrice(new BigDecimal(100.00));
persist(i);
commit();
int id = i.getItemId();
begin();
Item i2 = em.find(Item.class, id);
i2.setItemName("fancy cup");
em.flush();
em.clear();
//it is updated because it is flushed before clear();
assertEquals(1, query("select x from Item x "
+ "where x.itemName = 'fancy cup'").
getResultList().size());
i2.setItemPrice(new BigDecimal(120.00));
i2.setItemName("red cup");
em.merge(i2);
commit();
//Start a new transaction
begin();
//should be changed by previous commit
assertEquals(1, query("select x from Item x "
+ "where x.itemName = 'red cup' and x.itemPrice = 120.00").
getResultList().size());
// Rollback the transaction and close everything
rollback();
}
public static void main(String[] args) {
TestRunner.run(TestEntityManagerClear.class);
}