mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Remove PersistenceContextScope.NONE for now
This commit is contained in:
@@ -31,22 +31,5 @@ public enum PersistenceContextScope {
|
||||
* <p/>
|
||||
* You may use QUERY scope on a query that is executed in a transaction and you want to get a 'fresh copy' of the bean.
|
||||
*/
|
||||
QUERY,
|
||||
|
||||
/**
|
||||
* EXPERIMENTAL FEATURE - This is not expected to be used and somewhat experimental.
|
||||
* This NONE option effectively means that a PersistenceContext is not used when building the object graph and
|
||||
* subsequent lazy loading.
|
||||
* <p/>
|
||||
* You should ONLY use this when treating the resulting object graph as read only and even then you would be best
|
||||
* to use QUERY (or TRANSACTION).
|
||||
* <p/>
|
||||
* A query executed with NONE can build a object graph where there are multiple instances that represent the same
|
||||
* 'logical bean' by type and Id value (multiple instances of 'Customer 42'). Getting multiple instances that
|
||||
* represent the same logical bean (same row in the database) means that it is potentially dangerous/confusing to
|
||||
* use this scope when modifying the beans as multiple instances represent the same underlying rows in the database.
|
||||
* <p/>
|
||||
* Generally you would expect to always use TRANSACTION or QUERY scope.
|
||||
*/
|
||||
NONE
|
||||
QUERY
|
||||
}
|
||||
|
||||
@@ -198,18 +198,7 @@ public final class OrmQueryRequest<T> extends BeanRequest implements BeanQueryRe
|
||||
|
||||
// determine the scope (from the query and then server)
|
||||
PersistenceContextScope scope = ebeanServer.getPersistenceContextScope(query);
|
||||
switch (scope) {
|
||||
case QUERY:
|
||||
// Create a new PersistenceContext for this query
|
||||
return new DefaultPersistenceContext();
|
||||
case NONE:
|
||||
// Effectively don't use a PersistenceContext
|
||||
return new NoopPersistenceContext();
|
||||
default: {
|
||||
// Use the transaction scoped PersistenceContext
|
||||
return t.getPersistenceContext();
|
||||
}
|
||||
}
|
||||
return (scope == PersistenceContextScope.QUERY) ? new DefaultPersistenceContext() : t.getPersistenceContext();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-12
@@ -48,15 +48,6 @@ public class TestPersistenceContextQueryScope extends BaseTestCase {
|
||||
.setPersistenceContextScope(QUERY)
|
||||
.findUnique();
|
||||
|
||||
// NONE scope also hits the DB
|
||||
// ... also explicitly not use bean cache
|
||||
EBasicVer bean4 = Ebean.find(EBasicVer.class)
|
||||
.setId(bean.getId())
|
||||
.setUseCache(false) // ignore L2 cache
|
||||
.setPersistenceContextScope(NONE)
|
||||
.findUnique();
|
||||
|
||||
|
||||
// TRANsACTION scope ... same as bean2 and does not hit the DB
|
||||
EBasicVer bean5 = Ebean.find(EBasicVer.class)
|
||||
.setId(bean.getId())
|
||||
@@ -72,9 +63,7 @@ public class TestPersistenceContextQueryScope extends BaseTestCase {
|
||||
assertSame(bean1, bean5);
|
||||
|
||||
assertEquals("second", bean3.getName());
|
||||
assertEquals("second", bean4.getName());
|
||||
|
||||
Ebean.delete(bean4);
|
||||
Ebean.delete(bean3);
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
|
||||
+3
-32
@@ -13,40 +13,11 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.avaje.ebean.PersistenceContextScope.QUERY;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestPersistencContextNone extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_persistenceContextScopeNone() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
List<Order> orders = Ebean.find(Order.class)
|
||||
.setPersistenceContextScope(PersistenceContextScope.NONE)
|
||||
.fetch("customer", "id, name")
|
||||
.where().istartsWith("customer.name", "rob").eq("customer.id", 1)
|
||||
.orderBy().asc("customer.name")
|
||||
.findList();
|
||||
|
||||
assertTrue(!orders.isEmpty());
|
||||
|
||||
// collect the customer instances
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
Set<Integer> identities = new HashSet<Integer>();
|
||||
|
||||
for (Order order : orders) {
|
||||
Customer customer = order.getCustomer();
|
||||
identities.add(System.identityHashCode(customer));
|
||||
customers.add(customer);
|
||||
}
|
||||
|
||||
// Many instances of 'logically the same Customer'
|
||||
assertTrue(identities.size() > 1);
|
||||
assertEquals(identities.size(), customers.size());
|
||||
}
|
||||
public class TestPersistenceContextScopeUsingOrders extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_persistenceContextScopeQuery() {
|
||||
@@ -55,7 +26,7 @@ public class TestPersistencContextNone extends BaseTestCase {
|
||||
|
||||
List<Order> orders = Ebean.find(Order.class)
|
||||
// Use QUERY or TRANSACTION scope (just not NONE)
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.setPersistenceContextScope(QUERY)
|
||||
.fetch("customer", "id, name")
|
||||
.where().istartsWith("customer.name", "rob").eq("customer.id", 1)
|
||||
.orderBy().asc("customer.name")
|
||||
Reference in New Issue
Block a user