Search in sources :

Example 91 with ObjectId

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

the class DataContextIT method testInvalidateObjects_Vararg.

@Test
public void testInvalidateObjects_Vararg() throws Exception {
    DataRow row = new DataRow(10);
    row.put("ARTIST_ID", new Integer(1));
    row.put("ARTIST_NAME", "ArtistXYZ");
    row.put("DATE_OF_BIRTH", new Date());
    DataObject object = context.objectFromDataRow(Artist.class, row);
    ObjectId oid = object.getObjectId();
    // insert object into the ObjectStore
    context.getObjectStore().registerNode(oid, object);
    assertSame(object, context.getObjectStore().getNode(oid));
    assertNotNull(context.getObjectStore().getCachedSnapshot(oid));
    context.invalidateObjects(object);
    assertSame(oid, object.getObjectId());
    assertNull(context.getObjectStore().getCachedSnapshot(oid));
    assertSame(object, context.getObjectStore().getNode(oid));
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Example 92 with ObjectId

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

the class DataContextObjectIdQueryIT method testNoRefreshValuesNew.

@Test
public void testNoRefreshValuesNew() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    context.commitChanges();
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = 'Y'"));
    long id = Cayenne.longPKForObject(a);
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, id), false, ObjectIdQuery.CACHE);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    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) Test(org.junit.Test)

Example 93 with ObjectId

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

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicSharedCache_AfterCayenneInsert.

@Test
public void testPolymorphicSharedCache_AfterCayenneInsert() throws SQLException {
    // see CAY-2101... we are trying to get a snapshot from a new object in the shared cache, and then read this
    // object via a relationship, so that shared cache is consulted
    Employee e = context1.newObject(Employee.class);
    e.setName("E1");
    e.setSalary(1234.01f);
    context1.commitChanges();
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", Cayenne.intPKForObject(e)), false, ObjectIdQuery.CACHE);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

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

Example 94 with ObjectId

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

the class DataContextObjectTrackingIT method testInvalidateObjects_Vararg.

@Test
public void testInvalidateObjects_Vararg() {
    DataRow row = new DataRow(10);
    row.put("ARTIST_ID", new Integer(1));
    row.put("ARTIST_NAME", "ArtistXYZ");
    row.put("DATE_OF_BIRTH", new Date());
    DataObject obj = context.objectFromDataRow(Artist.class, row);
    ObjectId oid = obj.getObjectId();
    assertEquals(PersistenceState.COMMITTED, obj.getPersistenceState());
    assertSame(context, obj.getObjectContext());
    assertSame(obj, context.getGraphManager().getNode(oid));
    context.invalidateObjects(obj);
    assertEquals(PersistenceState.HOLLOW, obj.getPersistenceState());
    assertSame(context, obj.getObjectContext());
    assertSame(oid, obj.getObjectId());
    assertNull(context.getObjectStore().getCachedSnapshot(oid));
    assertNotNull(context.getGraphManager().getNode(oid));
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Example 95 with ObjectId

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

the class DataContextObjectTrackingIT method testUnregisterObject.

@Test
public void testUnregisterObject() {
    DataRow row = new DataRow(10);
    row.put("ARTIST_ID", new Integer(1));
    row.put("ARTIST_NAME", "ArtistXYZ");
    row.put("DATE_OF_BIRTH", new Date());
    DataObject obj = context.objectFromDataRow(Artist.class, row);
    ObjectId oid = obj.getObjectId();
    assertEquals(PersistenceState.COMMITTED, obj.getPersistenceState());
    assertSame(context, obj.getObjectContext());
    assertSame(obj, context.getGraphManager().getNode(oid));
    context.unregisterObjects(Collections.singletonList(obj));
    assertEquals(PersistenceState.TRANSIENT, obj.getPersistenceState());
    assertNull(obj.getObjectContext());
    assertEquals(oid, obj.getObjectId());
    assertNull(context.getGraphManager().getNode(oid));
    assertNull(context.getObjectStore().getCachedSnapshot(oid));
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) 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