use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class VerticalInheritanceMultipleAttributesIT method testUpdateTwoObjects.
/**
* @link https://issues.apache.org/jira/browse/CAY-2282
*/
@Test
public void testUpdateTwoObjects() throws SQLException {
// Insert records we want to update
ivOtherTable.insert(1, "other1");
ivOtherTable.insert(2, "other2");
ivBaseTable.insert(1, "Impl 1", "I");
ivBaseTable.insert(2, "Impl 2", "I");
ivImplTable.insert(1, "attr1", "attr2", 1, 2);
ivImplTable.insert(2, "attr1", "attr2", 1, 2);
// Fetch and update the records
IvOther other1 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other1")).selectOne(context);
IvOther other2 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other2")).selectOne(context);
List<IvImpl> implResult = ObjectSelect.query(IvImpl.class).select(context);
assertEquals(2, implResult.size());
for (IvImpl record : implResult) {
record.setName(record.getName() + "-Change");
record.setAttr1(record.getAttr1() + "-Change");
record.setAttr2(record.getAttr2() + "-Change");
record.setOther1(other2);
record.setOther2(other1);
}
context.commitChanges();
// Check result via clean context
ObjectContext cleanContext = runtime.newContext();
implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
assertEquals(2, implResult.size());
for (IvImpl record : implResult) {
assertTrue(record.getName().endsWith("-Change"));
assertTrue(record.getAttr1().endsWith("-Change"));
assertTrue(record.getAttr2().endsWith("-Change"));
assertEquals(other2.getObjectId(), record.getOther1().getObjectId());
assertEquals(other1.getObjectId(), record.getOther2().getObjectId());
}
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class PersistentObjectHolderTest method testInvalidate.
@Test
public void testInvalidate() {
ObjectContext context = mock(ObjectContext.class);
ClientMtTable2 o = new ClientMtTable2();
o.setPersistenceState(PersistenceState.COMMITTED);
o.setObjectContext(context);
PersistentObjectHolder holder = new PersistentObjectHolder(o, ClientMtTable2.TABLE1.getName());
assertTrue(holder.isFault());
ClientMtTable1 o1 = new ClientMtTable1();
o1.setObjectContext(context);
holder.setValueDirectly(o1);
holder.invalidate();
assertTrue(holder.isFault());
assertNull(holder.value);
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class ShallowMergeOperationIT method testMerge_NoOverride.
@Test
public void testMerge_NoOverride() throws Exception {
createArtistsDataSet();
ObjectContext childContext = runtime.newContext(context);
final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
int modifiedId = 33003;
final Artist modified = (Artist) Cayenne.objectForQuery(context, new ObjectIdQuery(ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
final Artist peerModified = (Artist) Cayenne.objectForQuery(childContext, new ObjectIdQuery(ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
modified.setArtistName("M1");
peerModified.setArtistName("M2");
assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, peerModified.getPersistenceState());
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
Persistent peerModified2 = op.merge(modified);
assertSame(peerModified, peerModified2);
assertEquals(PersistenceState.MODIFIED, peerModified2.getPersistenceState());
assertEquals("M2", peerModified.getArtistName());
assertEquals("M1", modified.getArtistName());
}
});
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class SessionContextRequestHandler method requestStart.
public void requestStart(ServletRequest request, ServletResponse response) {
CayenneRuntime.bindThreadInjector(injector);
if (request instanceof HttpServletRequest) {
// this forces session creation if it does not exist yet
HttpSession session = ((HttpServletRequest) request).getSession();
ObjectContext context;
synchronized (session) {
context = (ObjectContext) session.getAttribute(SESSION_CONTEXT_KEY);
if (context == null) {
context = injector.getInstance(ObjectContextFactory.class).createContext();
session.setAttribute(SESSION_CONTEXT_KEY, context);
}
}
BaseContext.bindThreadObjectContext(context);
}
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class ClientChannelServerDiffsIT method testReturnDiffInPrePersist.
@Test
public void testReturnDiffInPrePersist() {
final List<GenericDiff> diffs = new ArrayList<>();
final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler() {
@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
super.nodePropertyChanged(nodeId, property, oldValue, newValue);
diffs.add(new GenericDiff((ObjectId) nodeId, property, oldValue, newValue));
}
};
LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
try {
callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {
@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
assertNotNull(serverDiff);
serverDiff.apply(diffReader);
return serverDiff;
}
};
CayenneContext context = new CayenneContext(channel);
ClientMtTable1 o = context.newObject(ClientMtTable1.class);
ObjectId tempId = o.getObjectId();
o.setServerAttribute1("YY");
context.commitChanges();
assertEquals(2, diffReader.size);
assertEquals(1, diffs.size());
assertEquals(tempId, diffs.get(0).sourceId);
assertEquals(ClientMtTable1.GLOBAL_ATTRIBUTE1.getName(), diffs.get(0).property);
assertNull(diffs.get(0).oldValue);
assertEquals("XXX", diffs.get(0).newValue);
} finally {
callbackRegistry.clear();
}
}
Aggregations