Search in sources :

Example 6 with ArtistExhibit

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

the class DataContextPrefetchIT method testPrefetchToManyOnJoinTableJoinedPrefetch.

/**
 * Test that a to-many relationship is initialized when a target entity has
 * a compound PK only partially involved in relationship.
 */
@Test
public void testPrefetchToManyOnJoinTableJoinedPrefetch() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.joint());
    q.addOrdering(Artist.ARTIST_NAME.asc());
    final List<Artist> artists = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, artists.size());
            Artist a1 = artists.get(0);
            assertEquals("artist2", a1.getArtistName());
            List<?> toMany = (List<?>) a1.readPropertyDirectly("artistExhibitArray");
            assertNotNull(toMany);
            assertFalse(((ValueHolder) toMany).isFault());
            assertEquals(2, toMany.size());
            ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
            assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState());
            assertSame(a1, artistExhibit.getToArtist());
            Artist a2 = artists.get(1);
            assertEquals("artist3", a2.getArtistName());
            List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
            assertNotNull(toMany2);
            assertFalse(((ValueHolder) toMany2).isFault());
            assertEquals(3, toMany2.size());
            ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0);
            assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState());
            assertSame(a2, artistExhibit2.getToArtist());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 7 with ArtistExhibit

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

the class DataContextPrefetchIT method testPrefetchToMany_OnJoinTableDisjoinedPrefetch.

/**
 * Test that a to-many relationship is initialized when a target entity has
 * a compound PK only partially involved in relationship.
 */
@Test
public void testPrefetchToMany_OnJoinTableDisjoinedPrefetch() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.disjoint());
    q.addOrdering(Artist.ARTIST_NAME.asc());
    final List<Artist> artists = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, artists.size());
            Artist a1 = artists.get(0);
            assertEquals("artist2", a1.getArtistName());
            List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
            assertNotNull(toMany);
            assertFalse(((ValueHolder) toMany).isFault());
            assertEquals(2, toMany.size());
            ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
            assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState());
            assertSame(a1, artistExhibit.getToArtist());
            Artist a2 = artists.get(1);
            assertEquals("artist3", a2.getArtistName());
            List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
            assertNotNull(toMany2);
            assertFalse(((ValueHolder) toMany2).isFault());
            assertEquals(3, toMany2.size());
            ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0);
            assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState());
            assertSame(a2, artistExhibit2.getToArtist());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 8 with ArtistExhibit

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

the class DataContextPrefetchMultistepIT method testToManyToManyFirstStepUnresolved.

@Test
public void testToManyToManyFirstStepUnresolved() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    // since objects for the phantom prefetches are not retained explicitly, they may
    // get garbage collected, and we won't be able to detect them
    // so ensure ObjectStore uses a regular map just for this test
    context.getObjectStore().objectMap = new HashMap<Object, Persistent>();
    // Check the target ArtistExhibit objects do not exist yet
    Map<String, Object> id1 = new HashMap<>();
    id1.put("ARTIST_ID", 11);
    id1.put("EXHIBIT_ID", 2);
    ObjectId oid1 = new ObjectId("ArtistExhibit", id1);
    Map<String, Object> id2 = new HashMap<>();
    id2.put("ARTIST_ID", 101);
    id2.put("EXHIBIT_ID", 2);
    ObjectId oid2 = new ObjectId("ArtistExhibit", id2);
    assertNull(context.getGraphManager().getNode(oid1));
    assertNull(context.getGraphManager().getNode(oid2));
    Expression e = ExpressionFactory.exp("galleryName = $name");
    SelectQuery<Gallery> q = SelectQuery.query(Gallery.class, e.params(Collections.singletonMap("name", "gallery2")));
    q.addPrefetch("exhibitArray.artistExhibitArray");
    List<Gallery> galleries = context.select(q);
    assertEquals(1, galleries.size());
    Gallery g2 = galleries.get(0);
    // this relationship wasn't explicitly prefetched....
    Object list = g2.readPropertyDirectly("exhibitArray");
    assertTrue(list instanceof Fault);
    // however the target objects must be resolved
    ArtistExhibit ae1 = (ArtistExhibit) context.getGraphManager().getNode(oid1);
    ArtistExhibit ae2 = (ArtistExhibit) context.getGraphManager().getNode(oid2);
    assertNotNull(ae1);
    assertNotNull(ae2);
    assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, ae2.getPersistenceState());
}
Also used : HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Fault(org.apache.cayenne.Fault) Expression(org.apache.cayenne.exp.Expression) Gallery(org.apache.cayenne.testdo.testmap.Gallery) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) Test(org.junit.Test)

Example 9 with ArtistExhibit

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

the class DataContextPrefetchMultistepIT method testToManyToManyFirstStepResolved.

@Test
public void testToManyToManyFirstStepResolved() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    Expression e = ExpressionFactory.exp("galleryName = $name");
    SelectQuery<Gallery> q = SelectQuery.query(Gallery.class, e.params(Collections.singletonMap("name", "gallery2")));
    q.addPrefetch("exhibitArray");
    q.addPrefetch("exhibitArray.artistExhibitArray");
    List<Gallery> galleries = context.select(q);
    assertEquals(1, galleries.size());
    Gallery g2 = galleries.get(0);
    // this relationship should be resolved
    assertTrue(g2.readPropertyDirectly("exhibitArray") instanceof ValueHolder);
    List<Exhibit> exhibits = (List<Exhibit>) g2.readPropertyDirectly("exhibitArray");
    assertFalse(((ValueHolder) exhibits).isFault());
    assertEquals(1, exhibits.size());
    Exhibit e1 = exhibits.get(0);
    assertEquals(PersistenceState.COMMITTED, e1.getPersistenceState());
    // this to-many must also be resolved
    assertTrue(e1.readPropertyDirectly("artistExhibitArray") instanceof ValueHolder);
    List<ArtistExhibit> aexhibits = (List<ArtistExhibit>) e1.readPropertyDirectly("artistExhibitArray");
    assertFalse(((ValueHolder) aexhibits).isFault());
    assertEquals(1, exhibits.size());
    ArtistExhibit ae1 = aexhibits.get(0);
    assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
}
Also used : Expression(org.apache.cayenne.exp.Expression) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 10 with ArtistExhibit

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

the class CayenneDataObjectIT method testReadNestedPropertyToManyInMiddle.

@SuppressWarnings("unchecked")
@Test
public void testReadNestedPropertyToManyInMiddle() throws Exception {
    Artist a = context.newObject(Artist.class);
    ArtistExhibit ex = context.newObject(ArtistExhibit.class);
    Painting p1 = context.newObject(Painting.class);
    Painting p2 = context.newObject(Painting.class);
    p1.setPaintingTitle("p1");
    p2.setPaintingTitle("p2");
    a.addToPaintingArray(p1);
    a.addToPaintingArray(p2);
    ex.setToArtist(a);
    List<String> names = (List<String>) a.readNestedProperty("paintingArray.paintingTitle");
    assertEquals(names.size(), 2);
    assertEquals(names.get(0), "p1");
    assertEquals(names.get(1), "p2");
    List<String> names2 = (List<String>) ex.readNestedProperty("toArtist.paintingArray.paintingTitle");
    assertEquals(names, names2);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) ArrayList(java.util.ArrayList) List(java.util.List) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ArtistExhibit (org.apache.cayenne.testdo.testmap.ArtistExhibit)13 Test (org.junit.Test)13 List (java.util.List)9 Artist (org.apache.cayenne.testdo.testmap.Artist)9 ValueHolder (org.apache.cayenne.ValueHolder)7 Gallery (org.apache.cayenne.testdo.testmap.Gallery)7 Exhibit (org.apache.cayenne.testdo.testmap.Exhibit)6 Expression (org.apache.cayenne.exp.Expression)4 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)4 ArrayList (java.util.ArrayList)3 SelectQuery (org.apache.cayenne.query.SelectQuery)3 Painting (org.apache.cayenne.testdo.testmap.Painting)3 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Fault (org.apache.cayenne.Fault)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 ByteArrayTypeTest (org.apache.cayenne.access.types.ByteArrayTypeTest)1