Search in sources :

Example 41 with ObjectId

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateNotEqualID.

@Test
public void testProcessTemplateNotEqualID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE " + "COLUMN1 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN1')) " + "AND COLUMN2 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN2'))";
    Map<String, Object> idMap = new HashMap<>();
    idMap.put("ID_COLUMN1", 3);
    idMap.put("ID_COLUMN2", "aaa");
    ObjectId id = new ObjectId("T", idMap);
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(id);
    Map<String, Object> map = Collections.singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 <> ? AND COLUMN2 <> ?", compiled.getSql());
    assertEquals(2, compiled.getBindings().length);
    assertBindingValue(3, compiled.getBindings()[0]);
    assertBindingValue("aaa", compiled.getBindings()[1]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 42 with ObjectId

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

the class QualifierTranslatorIT method testExtras.

@Test
public void testExtras() throws Exception {
    ObjectId oid1 = new ObjectId("Gallery", "GALLERY_ID", 1);
    ObjectId oid2 = new ObjectId("Gallery", "GALLERY_ID", 2);
    Gallery g1 = new Gallery();
    Gallery g2 = new Gallery();
    g1.setObjectId(oid1);
    g2.setObjectId(oid2);
    Expression e1 = ExpressionFactory.matchExp("toGallery", g1);
    Expression e2 = e1.orExp(ExpressionFactory.matchExp("toGallery", g2));
    doExpressionTest(Exhibit.class, e2, "(ta.GALLERY_ID = ?) OR (ta.GALLERY_ID = ?)");
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Expression(org.apache.cayenne.exp.Expression) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Test(org.junit.Test)

Example 43 with ObjectId

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

the class FlattenedArcKeyIT method testHashCode.

@Test
public void testHashCode() {
    ObjectId src = new ObjectId("X");
    ObjectId target = new ObjectId("Y");
    ObjRelationship r1 = entityResolver.getObjEntity(FlattenedTest3.class).getRelationship(FlattenedTest3.TO_FT1.getName());
    FlattenedArcKey update = new FlattenedArcKey(src, target, r1);
    FlattenedArcKey update1 = new FlattenedArcKey(target, src, r1.getReverseRelationship());
    ObjRelationship r3 = entityResolver.getObjEntity(FlattenedTest1.class).getRelationship(FlattenedTest1.FT3OVER_COMPLEX.getName());
    FlattenedArcKey update2 = new FlattenedArcKey(target, src, r3);
    int h = update.hashCode();
    int h1 = update1.hashCode();
    int h2 = update2.hashCode();
    assertTrue(h == h1);
    assertTrue(h == update.hashCode());
    assertFalse(h == h2);
}
Also used : FlattenedTest3(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest3) ObjRelationship(org.apache.cayenne.map.ObjRelationship) FlattenedTest1(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1) ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 44 with ObjectId

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

the class IdentityColumnsIT method testCompoundPKWithGeneratedColumn.

@Test
public void testCompoundPKWithGeneratedColumn() throws Exception {
    if (adapter.supportsGeneratedKeys()) {
        // only works for generated keys, as the entity tested has one
        // Cayenne
        // auto-pk and one generated key
        String masterName = "m_" + System.currentTimeMillis();
        String depName1 = "dep1_" + System.currentTimeMillis();
        String depName2 = "dep2_" + System.currentTimeMillis();
        GeneratedColumnCompMaster master = context.newObject(GeneratedColumnCompMaster.class);
        master.setName(masterName);
        GeneratedColumnCompKey dep1 = context.newObject(GeneratedColumnCompKey.class);
        dep1.setName(depName1);
        dep1.setToMaster(master);
        GeneratedColumnCompKey dep2 = context.newObject(GeneratedColumnCompKey.class);
        dep2.setName(depName2);
        dep2.setToMaster(master);
        context.commitChanges();
        int masterId = Cayenne.intPKForObject(master);
        ObjectId id2 = dep2.getObjectId();
        // check propagated id
        Number propagatedID2 = (Number) id2.getIdSnapshot().get(GeneratedColumnCompKey.PROPAGATED_PK_PK_COLUMN);
        assertNotNull(propagatedID2);
        assertEquals(masterId, propagatedID2.intValue());
        // check Cayenne-generated ID
        Number cayenneGeneratedID2 = (Number) id2.getIdSnapshot().get(GeneratedColumnCompKey.AUTO_PK_PK_COLUMN);
        assertNotNull(cayenneGeneratedID2);
        // check DB-generated ID
        Number dbGeneratedID2 = (Number) id2.getIdSnapshot().get(GeneratedColumnCompKey.GENERATED_COLUMN_PK_COLUMN);
        assertNotNull(dbGeneratedID2);
        context.invalidateObjects(master, dep1, dep2);
        Object fetchedDep2 = Cayenne.objectForPK(context, id2);
        assertNotNull(fetchedDep2);
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GeneratedColumnCompMaster(org.apache.cayenne.testdo.generated.GeneratedColumnCompMaster) GeneratedColumnCompKey(org.apache.cayenne.testdo.generated.GeneratedColumnCompKey) Test(org.junit.Test)

Example 45 with ObjectId

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

the class JointPrefetchIT method testJointPrefetchMultiStep.

@Test
public void testJointPrefetchMultiStep() throws Exception {
    createJointPrefetchDataSet();
    // query with to-many joint prefetches
    SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).joint());
    final DataContext context = this.context;
    // make sure phantomly prefetched objects are not deallocated
    context.getObjectStore().objectMap = new HashMap<>();
    // sanity check...
    DataObject g1 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
    assertNull(g1);
    final List<Artist> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(3, objects.size());
        for (Artist a : objects) {
            ValueHolder list = (ValueHolder) a.getPaintingArray();
            assertNotNull(list);
            // intermediate relationship is not fetched...
            assertTrue(list.isFault());
        }
        // however both galleries must be in memory...
        DataObject g11 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
        assertNotNull(g11);
        assertEquals(PersistenceState.COMMITTED, g11.getPersistenceState());
        DataObject g2 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33002));
        assertNotNull(g2);
        assertEquals(PersistenceState.COMMITTED, g2.getPersistenceState());
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) ValueHolder(org.apache.cayenne.ValueHolder) 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