Search in sources :

Example 96 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class DataContextRefreshingIT method testInvalidateRootWithUpdatedAttributes.

@Test
public void testInvalidateRootWithUpdatedAttributes() throws Exception {
    createSingleArtistDataSet();
    String nameBefore = "artist2";
    String nameAfter = "not an artist";
    Artist artist = (Artist) context.performQuery(new SelectQuery(Artist.class)).get(0);
    assertNotNull(artist);
    assertEquals(nameBefore, artist.getArtistName());
    // update via DataNode directly
    assertEquals(1, tArtist.update().set("ARTIST_NAME", nameAfter).execute());
    context.invalidateObjects(artist);
    assertEquals(nameAfter, artist.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Example 97 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class DataContextRefreshingIT method testInvalidateRootWithNullToOneTargetChangedToNotNull.

@Test
public void testInvalidateRootWithNullToOneTargetChangedToNotNull() throws Exception {
    createSingleArtistAndUnrelatedPaintingDataSet();
    Painting painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
    assertNull(painting.getToArtist());
    assertEquals(1, tPainting.update().set("ARTIST_ID", 5).execute());
    context.invalidateObjects(painting);
    assertNotNull(painting.getToArtist());
    assertEquals("artist2", painting.getToArtist().getArtistName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 98 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class DataContextRollbackIT method testRollbackModifiedObject.

@Test
public void testRollbackModifiedObject() {
    String artistName = "initialTestArtist";
    Artist artist = (Artist) context.newObject("Artist");
    artist.setArtistName(artistName);
    context.commitChanges();
    artist.setArtistName("a new value");
    context.rollbackChanges();
    // Make sure the inmemory changes have been rolled back
    assertEquals(artistName, artist.getArtistName());
    // Commit what's in memory...
    context.commitChanges();
    // .. and ensure that the correct data is in the db
    DataContext freshContext = (DataContext) serverRuntime.newContext();
    assertNotSame(this.context, freshContext);
    SelectQuery query = new SelectQuery(Artist.class);
    query.setQualifier(ExpressionFactory.matchExp("artistName", artistName));
    List<?> queryResults = freshContext.performQuery(query);
    assertEquals(1, queryResults.size());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Example 99 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class DataContextRollbackIT method testRollbackWithMultipleNewObjects.

// Catches a bug where new objects were unregistered within an object iterator, thus
// modifying the collection the iterator was iterating over
// (ConcurrentModificationException)
@Test
public void testRollbackWithMultipleNewObjects() {
    String artistName = "rollbackTestArtist";
    String paintingTitle = "rollbackTestPainting";
    Artist artist = (Artist) context.newObject("Artist");
    artist.setArtistName(artistName);
    Painting painting = (Painting) context.newObject("Painting");
    painting.setPaintingTitle(paintingTitle);
    painting.setToArtist(artist);
    context.rollbackChanges();
    assertEquals(PersistenceState.TRANSIENT, artist.getPersistenceState());
    context.commitChanges();
    // The commit should have made no changes, so
    // perform a fetch to ensure that this artist hasn't been persisted to the db
    DataContext freshContext = (DataContext) serverRuntime.newContext();
    assertNotSame(this.context, freshContext);
    SelectQuery query = new SelectQuery(Artist.class);
    query.setQualifier(ExpressionFactory.matchExp("artistName", artistName));
    List<?> queryResults = freshContext.performQuery(query);
    assertEquals(0, queryResults.size());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 100 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class DataContextSelectQuerySplitAliasesIT method testAliasPathSplits_SplitJoin.

@Test
public void testAliasPathSplits_SplitJoin() throws Exception {
    createTwoArtistsThreePaintingsDataSet();
    SelectQuery query = new SelectQuery(Artist.class);
    query.andQualifier(ExpressionFactory.matchExp("p1.paintingTitle", "X"));
    query.andQualifier(ExpressionFactory.matchExp("p2.paintingTitle", "Y"));
    query.aliasPathSplits("paintingArray", "p1", "p2");
    List<Artist> artists = context.performQuery(query);
    assertEquals(1, artists.size());
    assertEquals("BB", artists.get(0).getArtistName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)360 Test (org.junit.Test)348 Artist (org.apache.cayenne.testdo.testmap.Artist)128 Painting (org.apache.cayenne.testdo.testmap.Painting)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)64 Expression (org.apache.cayenne.exp.Expression)47 List (java.util.List)43 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)26 ValueHolder (org.apache.cayenne.ValueHolder)21 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)13 DataRow (org.apache.cayenne.DataRow)12 ObjectContext (org.apache.cayenne.ObjectContext)12 DbEntity (org.apache.cayenne.map.DbEntity)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)11 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)10 Query (org.apache.cayenne.query.Query)10