Search in sources :

Example 26 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextRefreshingIT method testRefetchRootWithNullifiedToOne.

@Test
public void testRefetchRootWithNullifiedToOne() throws Exception {
    createSingleArtistAndPaintingDataSet();
    Painting painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
    assertNotNull(painting.getToArtist());
    assertEquals("artist2", painting.getToArtist().getArtistName());
    assertEquals(1, tPainting.update().set("ARTIST_ID", null, Types.BIGINT).execute());
    // select without prefetch
    painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
    assertNotNull(painting);
    assertNull(painting.getToArtist());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 27 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextRefreshingIT method testInvalidateRootWithChangedToOneTarget.

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

Example 28 with Painting

use of org.apache.cayenne.testdo.testmap.Painting 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 29 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextRelationshipQueryIT method testUnrefreshingToOne.

@Test
public void testUnrefreshingToOne() throws Exception {
    createOneArtistOnePaintingDataSet();
    Painting p = Cayenne.objectForPK(context, Painting.class, 1);
    // resolve artist once before running non-refreshing query, to check that we do
    // not refresh the object
    Artist a = Cayenne.objectForPK(context, Artist.class, 1);
    long v = a.getSnapshotVersion();
    int writeCalls = a.getPropertyWrittenDirectly();
    assertEquals("a1", a.getArtistName());
    assertEquals(1, tArtist.update().set("ARTIST_NAME", "a2").where("ARTIST_ID", 1).execute());
    RelationshipQuery toOne = new RelationshipQuery(p.getObjectId(), Painting.TO_ARTIST.getName(), false);
    List<Artist> related = context.performQuery(toOne);
    assertEquals(1, related.size());
    assertTrue(related.contains(a));
    assertEquals("a1", a.getArtistName());
    assertEquals(v, a.getSnapshotVersion());
    assertEquals("Looks like relationship query caused snapshot refresh", writeCalls, a.getPropertyWrittenDirectly());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 30 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextRelationshipQueryIT method testRefreshingToOne.

@Test
public void testRefreshingToOne() throws Exception {
    createOneArtistOnePaintingDataSet();
    Painting p = Cayenne.objectForPK(context, Painting.class, 1);
    // resolve artist once before running non-refreshing query, to check that we do
    // not refresh the object
    Artist a = Cayenne.objectForPK(context, Artist.class, 1);
    long v = a.getSnapshotVersion();
    int writeCalls = a.getPropertyWrittenDirectly();
    assertEquals("a1", a.getArtistName());
    assertEquals(1, tArtist.update().set("ARTIST_NAME", "a2").where("ARTIST_ID", 1).execute());
    RelationshipQuery toOne = new RelationshipQuery(p.getObjectId(), Painting.TO_ARTIST.getName(), true);
    List<Artist> related = context.performQuery(toOne);
    assertEquals(1, related.size());
    assertTrue(related.contains(a));
    assertEquals("a2", a.getArtistName());
    assertTrue("Looks like relationship query didn't cause a snapshot refresh", v < a.getSnapshotVersion());
    assertTrue("Looks like relationship query didn't cause a snapshot refresh", writeCalls < a.getPropertyWrittenDirectly());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

Painting (org.apache.cayenne.testdo.testmap.Painting)222 Test (org.junit.Test)218 Artist (org.apache.cayenne.testdo.testmap.Artist)144 SelectQuery (org.apache.cayenne.query.SelectQuery)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)35 List (java.util.List)30 Expression (org.apache.cayenne.exp.Expression)29 ArrayList (java.util.ArrayList)21 BigDecimal (java.math.BigDecimal)19 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)14 ObjectContext (org.apache.cayenne.ObjectContext)13 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)13 ValueHolder (org.apache.cayenne.ValueHolder)12 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 DataRow (org.apache.cayenne.DataRow)7 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)6 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)6 RefreshQuery (org.apache.cayenne.query.RefreshQuery)5