Search in sources :

Example 26 with ObjectIdQuery

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

the class DataContextEntityWithMeaningfulPKIT method testInsertWithMeaningfulPK.

@Test
public void testInsertWithMeaningfulPK() {
    MeaningfulPKTest1 obj = context.newObject(MeaningfulPKTest1.class);
    obj.setPkAttribute(1000);
    obj.setDescr("aaa-aaa");
    context.commitChanges();
    ObjectId objId = new ObjectId("MeaningfulPKTest1", MeaningfulPKTest1.PK_ATTRIBUTE_PK_COLUMN, 1000);
    ObjectIdQuery q = new ObjectIdQuery(objId, true, ObjectIdQuery.CACHE_REFRESH);
    @SuppressWarnings("unchecked") List<DataRow> result = (List<DataRow>) context.performQuery(q);
    assertEquals(1, result.size());
    assertEquals(1000, result.get(0).get(MeaningfulPKTest1.PK_ATTRIBUTE_PK_COLUMN));
}
Also used : MeaningfulPKTest1(org.apache.cayenne.testdo.meaningful_pk.MeaningfulPKTest1) ObjectId(org.apache.cayenne.ObjectId) List(java.util.List) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 27 with ObjectIdQuery

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

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

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

the class CompoundPkChangeIT method testCompoundPkChangeSingleElement.

@Test
public void testCompoundPkChangeSingleElement() throws Exception {
    CompoundPkTestEntity object = context.newObject(CompoundPkTestEntity.class);
    CompoundPkTestEntity refreshedObject = null;
    object.setKey1(key1v1);
    object.setKey2(key2v1);
    object.setName("testing testing");
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey2(key2v2);
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v2, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch1 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch1);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey2(key2v3);
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v3, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch2 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch2);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 30 with ObjectIdQuery

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

the class CompoundPkChangeIT method testCompoundPkChangeAllElements.

@Test
public void testCompoundPkChangeAllElements() throws Exception {
    CompoundPkTestEntity object = context.newObject(CompoundPkTestEntity.class);
    CompoundPkTestEntity refreshedObject = null;
    object.setKey1(key1v1);
    object.setKey2(key2v1);
    object.setName("testing testing");
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey1(key1v2);
    object.setKey2(key2v2);
    context.commitChanges();
    assertEquals(key1v2, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v2, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch1 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch1);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey1(key1v3);
    object.setKey2(key2v3);
    context.commitChanges();
    assertEquals(key1v3, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v3, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch2 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch2);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) 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