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 = getSQLTemplateBuilder()
.createSQLTemplate(
Artist.class,
"UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)");
Map map = new HashMap(3);
map.put("newName", newName);
map.put("oldName", originalName);
update.setParameters(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());
}