Search in sources :

Example 26 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class EJBQLQueryIT method testNullObjectsCallback.

@Test
public void testNullObjectsCallback() throws Exception {
    tArtist.insert(1, "a1");
    tArtist.insert(2, "a2");
    tArtist.insert(3, "a3");
    tPainting.insert(1, 2, "title1");
    tPainting.insert(2, 1, "title2");
    tPainting.insert(3, 1, "title3");
    // set callback to be called
    LifecycleCallbackRegistry registry = runtime.getDataDomain().getEntityResolver().getCallbackRegistry();
    registry.addCallback(LifecycleEvent.POST_LOAD, Painting.class, "postAddCallback");
    // select Paintings, where one of it will be null
    EJBQLQuery query = new EJBQLQuery("select a.paintingArray+ from Artist a order by a.artistName");
    List<Painting> result1 = context.performQuery(query);
    assertEquals(4, result1.size());
    assertNull(result1.get(3));
    for (int i = 0; i < 3; i++) {
        assertNotNull(result1.get(i));
        assertTrue(result1.get(i).isPostAdded());
    }
}
Also used : LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 27 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPostAddCallback.

@Test
public void testPostAddCallback() throws Exception {
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    final boolean[] flag = new boolean[1];
    try {
        callbackRegistry.addListener(MtTable1.class, new LifecycleListener() {

            public void postLoad(Object entity) {
            }

            public void postPersist(Object entity) {
            }

            public void postRemove(Object entity) {
            }

            public void postUpdate(Object entity) {
            }

            public void postAdd(Object entity) {
                flag[0] = true;
            }

            public void preRemove(Object entity) {
            }

            public void preUpdate(Object entity) {
            }

            public void prePersist(Object entity) {
            }
        });
        clientContext.newObject(ClientMtTable1.class);
        assertFalse(flag[0]);
        clientContext.commitChanges();
        assertTrue(flag[0]);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Aggregations

LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)27 Test (org.junit.Test)26 Artist (org.apache.cayenne.testdo.testmap.Artist)15 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 Painting (org.apache.cayenne.testdo.testmap.Painting)6 SelectQuery (org.apache.cayenne.query.SelectQuery)4 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)4 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)2 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)2 ArrayList (java.util.ArrayList)1 CayenneContext (org.apache.cayenne.CayenneContext)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DataContext (org.apache.cayenne.access.DataContext)1 MockEventManager (org.apache.cayenne.event.MockEventManager)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 RefreshQuery (org.apache.cayenne.query.RefreshQuery)1 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)1 MtTable1 (org.apache.cayenne.testdo.mt.MtTable1)1