Search in sources :

Example 16 with ObjectIdQuery

use of org.apache.cayenne.query.ObjectIdQuery 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 17 with ObjectIdQuery

use of org.apache.cayenne.query.ObjectIdQuery 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 18 with ObjectIdQuery

use of org.apache.cayenne.query.ObjectIdQuery 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 19 with ObjectIdQuery

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

the class CDOOneToOneFKIT method testTakeObjectSnapshotDependentFault.

@Test
public void testTakeObjectSnapshotDependentFault() throws Exception {
    ToOneFK2 src = context.newObject(ToOneFK2.class);
    ToOneFK1 target = context.newObject(ToOneFK1.class);
    src.setToOneToFK(target);
    context.commitChanges();
    ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
    assertTrue(src2.readPropertyDirectly("toOneToFK") instanceof Fault);
    // test that taking a snapshot does not trigger a fault, and generally works well
    context.currentSnapshot(src2);
    assertTrue(src2.readPropertyDirectly("toOneToFK") instanceof Fault);
}
Also used : ToOneFK2(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2) ToOneFK1(org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 20 with ObjectIdQuery

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

the class CayenneIT method testObjectForQuery.

@Test
public void testObjectForQuery() throws Exception {
    createOneArtist();
    ObjectId id = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, new Integer(33002));
    assertNull(context.getGraphManager().getNode(id));
    Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
    assertNotNull(object);
    assertTrue(object instanceof Artist);
    assertEquals("artist2", ((Artist) object).getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Aggregations

ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)42 Test (org.junit.Test)34 ObjectId (org.apache.cayenne.ObjectId)15 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)11 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 ToOneFK2 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2)6 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)6 DataRow (org.apache.cayenne.DataRow)5 Artist (org.apache.cayenne.testdo.testmap.Artist)5 ToOneFK1 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1)4 List (java.util.List)3 ObjectContext (org.apache.cayenne.ObjectContext)3 Persistent (org.apache.cayenne.Persistent)3 QueryResponse (org.apache.cayenne.QueryResponse)3 SQLTemplate (org.apache.cayenne.query.SQLTemplate)3 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)3 Date (java.util.Date)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2