mirror of https://github.com/apache/openjpa.git
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:
parent
baa01d79dd
commit
0226888590
|
@ -3168,7 +3168,7 @@ public class BrokerImpl
|
||||||
try {
|
try {
|
||||||
// If a flush is desired (based on input parm), then check if the
|
// If a flush is desired (based on input parm), then check if the
|
||||||
// "dirty" flag is set before calling flush().
|
// "dirty" flag is set before calling flush().
|
||||||
if ((flush) && ((_flags & FLAG_FLUSH_REQUIRED) != 0))
|
if (flush && (_flags & FLAG_FLUSH_REQUIRED) != 0)
|
||||||
flush();
|
flush();
|
||||||
detachAllInternal(call);
|
detachAllInternal(call);
|
||||||
} catch (OpenJPAException ke) {
|
} catch (OpenJPAException ke) {
|
||||||
|
|
|
@ -446,7 +446,8 @@ public class DetachManager
|
||||||
// detach fields and set detached variables
|
// detach fields and set detached variables
|
||||||
DetachedStateManager detSM = null;
|
DetachedStateManager detSM = null;
|
||||||
if (_opts.getDetachedStateManager()
|
if (_opts.getDetachedStateManager()
|
||||||
&& useDetachedStateManager(sm, _opts))
|
&& useDetachedStateManager(sm, _opts)
|
||||||
|
&& !(sm.isNew() && !sm.isDeleted() && !sm.isFlushed()))
|
||||||
detSM = new DetachedStateManager(detachedPC, sm, fields,
|
detSM = new DetachedStateManager(detachedPC, sm, fields,
|
||||||
_opts.getAccessUnloaded(), _broker.getMultithreaded());
|
_opts.getAccessUnloaded(), _broker.getMultithreaded());
|
||||||
if (_full) {
|
if (_full) {
|
||||||
|
|
|
@ -79,6 +79,9 @@ public class DetachedStateManager
|
||||||
_embedded = sm.isEmbedded();
|
_embedded = sm.isEmbedded();
|
||||||
_loaded = load;
|
_loaded = load;
|
||||||
_access = access;
|
_access = access;
|
||||||
|
if (!sm.isFlushed())
|
||||||
|
_dirty = (BitSet) sm.getDirty().clone();
|
||||||
|
else
|
||||||
_dirty = new BitSet(_loaded.length());
|
_dirty = new BitSet(_loaded.length());
|
||||||
_oid = sm.fetchObjectId();
|
_oid = sm.fetchObjectId();
|
||||||
_version = sm.getVersion();
|
_version = sm.getVersion();
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.simple;
|
||||||
|
|
||||||
import junit.textui.TestRunner;
|
import junit.textui.TestRunner;
|
||||||
import org.apache.openjpa.persistence.test.SingleEMTestCase;
|
import org.apache.openjpa.persistence.test.SingleEMTestCase;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case to ensure that the proper JPA clear semantics are processed.
|
* Test case to ensure that the proper JPA clear semantics are processed.
|
||||||
|
@ -30,7 +31,7 @@ public class TestEntityManagerClear
|
||||||
extends SingleEMTestCase {
|
extends SingleEMTestCase {
|
||||||
|
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
setUp(AllFieldTypes.class);
|
setUp(AllFieldTypes.class, Item.class,CLEAR_TABLES);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDetach() {
|
public void testDetach() {
|
||||||
|
@ -94,6 +95,148 @@ public class TestEntityManagerClear
|
||||||
rollback();
|
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) {
|
public static void main(String[] args) {
|
||||||
TestRunner.run(TestEntityManagerClear.class);
|
TestRunner.run(TestEntityManagerClear.class);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue