Search in sources :

Example 66 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesNew.

@Test
public void testRefreshNullifiedValuesNew() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    a.setDateOfBirth(new Date());
    context.commitChanges();
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
    long id = Cayenne.longPKForObject(a);
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, id), false, ObjectIdQuery.CACHE_REFRESH);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    assertNull(a1.getDateOfBirth());
    assertEquals("X", a1.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Date(java.util.Date) Test(org.junit.Test)

Example 67 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesExisting.

@Test
public void testRefreshNullifiedValuesExisting() {
    SQLTemplate insert = new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) VALUES (44, 'X', #bind($date 'DATE'))");
    insert.setParameters(Collections.singletonMap("date", new Date()));
    context.performGenericQuery(insert);
    Artist a = Cayenne.objectForPK(context, Artist.class, 44l);
    assertNotNull(a.getDateOfBirth());
    assertEquals("X", a.getArtistName());
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 44l), false, ObjectIdQuery.CACHE_REFRESH);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    assertNull(a1.getDateOfBirth());
    assertEquals("X", a1.getArtistName());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Date(java.util.Date) Test(org.junit.Test)

Example 68 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicSharedCache.

@Test
public void testPolymorphicSharedCache() throws SQLException {
    tPerson.insert(1, "P1", "EM");
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", 1), false, ObjectIdQuery.CACHE);
    AbstractPerson ap1 = (AbstractPerson) Cayenne.objectForQuery(context1, q1);
    assertTrue(ap1 instanceof Manager);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            // use different context to ensure we hit shared cache
            AbstractPerson ap2 = (AbstractPerson) Cayenne.objectForQuery(context2, q1);
            assertTrue(ap2 instanceof Manager);
        }
    });
}
Also used : ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 69 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicLocalCache.

@Test
public void testPolymorphicLocalCache() throws SQLException {
    tPerson.insert(1, "P1", "EM");
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", 1), false, ObjectIdQuery.CACHE);
    AbstractPerson ap1 = (AbstractPerson) Cayenne.objectForQuery(context1, q1);
    assertTrue(ap1 instanceof Manager);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            // use same context to ensure we hit local cache
            // note that this does not guarantee test correctness. If local
            // cache polymorphic ID lookup is broken, shared cache will pick
            // it up
            AbstractPerson ap2 = (AbstractPerson) Cayenne.objectForQuery(context1, q1);
            assertTrue(ap2 instanceof Manager);
        }
    });
}
Also used : ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 70 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextCommitIT method testFlushToParent_Commit_Mix.

@Test
public void testFlushToParent_Commit_Mix() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("Test");
    context.flushToParent(true);
    // commit a mix of new and modified
    Painting p = context.newObject(Painting.class);
    p.setPaintingTitle("PT");
    p.setToArtist(a);
    a.setArtistName("Test_");
    ObjectId beforeId = p.getObjectId();
    GraphDiff diff = context.flushToParent(true);
    ObjectId afterId = p.getObjectId();
    assertNotNull(diff);
    assertFalse(context.hasChanges());
    GraphChangeHandler diffChecker = mock(GraphChangeHandler.class);
    diff.apply(diffChecker);
    verify(diffChecker).nodeIdChanged(beforeId, afterId);
    verifyZeroInteractions(diffChecker);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)156 Test (org.junit.Test)104 Persistent (org.apache.cayenne.Persistent)38 DataObject (org.apache.cayenne.DataObject)20 DataRow (org.apache.cayenne.DataRow)20 ObjectContext (org.apache.cayenne.ObjectContext)20 ObjEntity (org.apache.cayenne.map.ObjEntity)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)18 HashMap (java.util.HashMap)17 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)16 ArrayList (java.util.ArrayList)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)14 Artist (org.apache.cayenne.testdo.testmap.Artist)14 Map (java.util.Map)13 EntityResolver (org.apache.cayenne.map.EntityResolver)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)10 SelectQuery (org.apache.cayenne.query.SelectQuery)10 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)9