mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Fix for #207: Add support for PeristenceContextScope QUERY and NONE (in addition to the existing TRANSACTION scope)
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package com.avaje.tests.persistencecontext;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.PersistenceContextScope;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_persistenceContextScopeQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> orders = Ebean.find(Order.class)
|
||||
// Use QUERY or TRANSACTION scope (just not NONE)
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.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);
|
||||
}
|
||||
|
||||
// only one instance of Customer this time ...
|
||||
assertEquals(1, identities.size());
|
||||
assertTrue(customers.size() > 1);
|
||||
}
|
||||
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.avaje.tests.persistencecontext;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.EBasicVer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.avaje.ebean.PersistenceContextScope.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
|
||||
public class TestPersistenceContextQueryScope extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("first");
|
||||
Ebean.save(bean);
|
||||
|
||||
//Ebean.getServerCacheManager().setCaching(EBasicVer.class, true);
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
EBasicVer bean1 = Ebean.find(EBasicVer.class, bean.getId());
|
||||
|
||||
// do an update of the name
|
||||
EBasicVer updateBean = new EBasicVer();
|
||||
updateBean.setId(bean.getId());
|
||||
updateBean.setName("second");
|
||||
|
||||
// perform an update to the DB
|
||||
Ebean.update(updateBean);
|
||||
|
||||
// fetch the bean again... but doesn't hit DB as it
|
||||
// is in the PersistenceContext which is transaction scoped
|
||||
EBasicVer bean2 = Ebean.find(EBasicVer.class)
|
||||
.setId(bean.getId())
|
||||
.setUseCache(false) // ignore L2 cache
|
||||
.findUnique();
|
||||
|
||||
// QUERY scope hits the DB (doesn't use the existing transactions persistence context)
|
||||
// ... also explicitly not use bean cache
|
||||
EBasicVer bean3 = Ebean.find(EBasicVer.class)
|
||||
.setId(bean.getId())
|
||||
.setUseCache(false) // ignore L2 cache
|
||||
.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())
|
||||
.setUseCache(false) // ignore L2 cache
|
||||
.setPersistenceContextScope(TRANSACTION)
|
||||
.findUnique();
|
||||
|
||||
assertEquals("first", bean.getName());
|
||||
assertEquals("first", bean1.getName());
|
||||
assertEquals("first", bean2.getName());
|
||||
assertEquals("first", bean5.getName());
|
||||
assertSame(bean1, bean2);
|
||||
assertSame(bean1, bean5);
|
||||
|
||||
assertEquals("second", bean3.getName());
|
||||
assertEquals("second", bean4.getName());
|
||||
|
||||
Ebean.delete(bean4);
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.avaje.tests.persistencecontext;
|
||||
|
||||
import com.avaje.ebean.*;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.tests.model.basic.EBasicVer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestPersistenceContextServerConfig extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_config() {
|
||||
|
||||
SpiEbeanServer ebeanServer = (SpiEbeanServer)create();
|
||||
|
||||
Query<EBasicVer> query = ebeanServer.find(EBasicVer.class);
|
||||
|
||||
PersistenceContextScope scope = ebeanServer.getPersistenceContextScope((SpiQuery<?>) query);
|
||||
|
||||
assertEquals(PersistenceContextScope.QUERY, scope);
|
||||
}
|
||||
|
||||
static EbeanServer create() {
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("h2");
|
||||
config.loadFromProperties();
|
||||
config.setName("withPCQuery");
|
||||
config.setPersistenceContextScope(PersistenceContextScope.QUERY);
|
||||
|
||||
config.addClass(EBasicVer.class);
|
||||
|
||||
return EbeanServerFactory.create(config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user