use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class NumericTypesIT method testBooleanBoolean.
@Test
public void testBooleanBoolean() throws Exception {
// populate (testing insert as well)
BooleanTestEntity trueObject = (BooleanTestEntity) context.newObject("BooleanTestEntity");
trueObject.setBooleanColumn(Boolean.TRUE);
BooleanTestEntity falseObject = (BooleanTestEntity) context.newObject("BooleanTestEntity");
falseObject.setBooleanColumn(Boolean.FALSE);
context.commitChanges();
context.invalidateObjects(trueObject, falseObject);
// fetch true...
Expression trueQ = ExpressionFactory.matchExp("booleanColumn", Boolean.TRUE);
List<?> trueResult = context1.performQuery(new SelectQuery<>(BooleanTestEntity.class, trueQ));
assertEquals(1, trueResult.size());
BooleanTestEntity trueRefetched = (BooleanTestEntity) trueResult.get(0);
assertEquals(Boolean.TRUE, trueRefetched.getBooleanColumn());
// CAY-320. Simplifying the use of booleans to allow identity comparison.
assertNotSame(trueRefetched, trueObject);
assertSame(Boolean.TRUE, trueRefetched.getBooleanColumn());
// fetch false
Expression falseQ = ExpressionFactory.matchExp("booleanColumn", Boolean.FALSE);
List<?> falseResult = context1.performQuery(new SelectQuery<>(BooleanTestEntity.class, falseQ));
assertEquals(1, falseResult.size());
BooleanTestEntity falseRefetched = (BooleanTestEntity) falseResult.get(0);
assertEquals(Boolean.FALSE, falseRefetched.getBooleanColumn());
// CAY-320. Simplifying the use of booleans to allow identity comparison.
assertNotSame(falseRefetched, falseObject);
assertSame(Boolean.FALSE, falseRefetched.getBooleanColumn());
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class NumericTypesIT method testTinyintInQualifier.
@Test
public void testTinyintInQualifier() throws Exception {
createTinyintDataSet();
// test
Expression qual = ExpressionFactory.matchExp("tinyintCol", (byte) 81);
List<?> objects = context.performQuery(new SelectQuery<>(TinyintTestEntity.class, qual));
assertEquals(1, objects.size());
TinyintTestEntity object = (TinyintTestEntity) objects.get(0);
assertEquals(new Byte((byte) 81), object.getTinyintCol());
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultsLocalCache.
@Test
public void testRefreshQueryResultsLocalCache() throws Exception {
createRefreshCollectionDataSet();
Expression qual = Painting.PAINTING_TITLE.eq("P2");
SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
q.setCacheGroup("X");
List<?> paints = context.performQuery(q);
// fetch P1 separately from cached query
Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
Painting p2 = (Painting) paints.get(0);
Artist a1 = p2.getToArtist();
assertSame(a1, p1.getToArtist());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
createRefreshCollectionToOneUpdateDataSet();
RefreshQuery refresh = new RefreshQuery(q);
context.performQuery(refresh);
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
// probably refreshed eagerly
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
assertSame(a1, p1.getToArtist());
assertNotSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("b", p2.getToArtist().getArtistName());
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultGroupLocal.
@Test
public void testRefreshQueryResultGroupLocal() throws Exception {
createRefreshCollectionDataSet();
Expression qual = Painting.PAINTING_TITLE.eq("P2");
SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
q.setCacheGroup("X");
List<?> paints = context.performQuery(q);
// fetch P1 separately from cached query
Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
Painting p2 = (Painting) paints.get(0);
Artist a1 = p2.getToArtist();
assertSame(a1, p1.getToArtist());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
createRefreshCollectionToOneUpdateDataSet();
// results are served from cache and therefore are not refreshed
context.performQuery(q);
assertSame(a1, p1.getToArtist());
assertSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("c", p2.getToArtist().getArtistName());
RefreshQuery refresh = new RefreshQuery("X");
// this should invalidate results for the next query run
context.performQuery(refresh);
// this should force a refresh
context.performQuery(q);
assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
assertSame(a1, p1.getToArtist());
assertNotSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("b", p2.getToArtist().getArtistName());
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class DataContextSharedCacheIT method testCacheRefreshingOnSelect.
/**
* Checks that cache is refreshed when a query "refreshingObjects" property is set to
* true.
*/
@Test
public void testCacheRefreshingOnSelect() throws Exception {
String originalName = artist.getArtistName();
final String newName = "version2";
DataContext context = (DataContext) artist.getObjectContext();
DataRow oldSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
assertNotNull(oldSnapshot);
assertEquals(originalName, oldSnapshot.get("ARTIST_NAME"));
// update artist using raw SQL
SQLTemplate update = sqlTemplateCustomizer.createSQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)");
Map<String, Object> map = new HashMap<>(3);
map.put("newName", newName);
map.put("oldName", originalName);
update.setParams(map);
context.performNonSelectingQuery(update);
// fetch updated artist without refreshing
Expression qual = ExpressionFactory.matchExp("artistName", newName);
SelectQuery query = new SelectQuery<>(Artist.class, qual);
List artists = context.performQuery(query);
assertEquals(1, artists.size());
artist = (Artist) artists.get(0);
// check underlying cache
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
assertNotSame(oldSnapshot, freshSnapshot);
assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
// check an artist
assertEquals(newName, artist.getArtistName());
}
Aggregations