Search in sources :

Example 76 with ObjectId

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

the class ChildDiffLoader method nodeCreated.

public void nodeCreated(Object nodeId) {
    setExternalChange(Boolean.TRUE);
    try {
        ObjectId id = (ObjectId) nodeId;
        if (id.getEntityName() == null) {
            throw new NullPointerException("Null entity name in id " + id);
        }
        ObjEntity entity = context.getEntityResolver().getObjEntity(id.getEntityName());
        if (entity == null) {
            throw new IllegalArgumentException("Entity not mapped with Cayenne: " + id);
        }
        Persistent dataObject;
        try {
            dataObject = (Persistent) entity.getJavaClass().newInstance();
        } catch (Exception ex) {
            throw new CayenneRuntimeException("Error instantiating object.", ex);
        }
        dataObject.setObjectId(id);
        context.registerNewObject(dataObject);
    } finally {
        setExternalChange(Boolean.FALSE);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 77 with ObjectId

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

the class FlattenedArcKeyIT method testAttributes.

@Test
public void testAttributes() {
    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);
    assertSame(src, update.id1.getSourceId());
    assertSame(target, update.id2.getSourceId());
    assertSame(r1, update.relationship);
}
Also used : FlattenedTest3(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest3) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 78 with ObjectId

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

the class FlattenedArcKeyIT method testEquals.

@Test
public void testEquals() {
    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);
    assertTrue(update.equals(update1));
    assertFalse(update.equals(update2));
}
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 79 with ObjectId

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

the class IdentityColumnsIT method testCAY823.

/**
 * Tests a bug casued by the ID Java type mismatch vs the default JDBC type
 * of the ID column.
 */
@Test
public void testCAY823() throws Exception {
    GeneratedColumnTestEntity idObject = context.newObject(GeneratedColumnTestEntity.class);
    String name = "n_" + System.currentTimeMillis();
    idObject.setName(name);
    idObject.getObjectContext().commitChanges();
    ObjectId id = idObject.getObjectId();
    context.invalidateObjects(idObject);
    SelectQuery q = new SelectQuery(GeneratedColumnTestEntity.class);
    q.setPageSize(10);
    List<?> results = context.performQuery(q);
    assertEquals(1, results.size());
    // per CAY-823 an attempt to resolve an object results in an exception
    assertEquals(id, ((Persistent) results.get(0)).getObjectId());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ObjectId(org.apache.cayenne.ObjectId) GeneratedColumnTestEntity(org.apache.cayenne.testdo.generated.GeneratedColumnTestEntity) Test(org.junit.Test)

Example 80 with ObjectId

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

the class NestedDataContextReadIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    createPrefetchingDataSet();
    final ObjectContext child = runtime.newContext(context);
    final ObjectId prefetchedId = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, new Integer(33001));
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.addOrdering(Painting.PAINTING_TITLE.asc());
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    final List<Painting> results = q.select(child);
    // blockQueries();
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(2, results.size());
        for (Painting o : results) {
            assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
            assertSame(child, o.getObjectContext());
            Artist o1 = o.getToArtist();
            assertNotNull(o1);
            assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
            assertSame(child, o1.getObjectContext());
            assertEquals(prefetchedId, o1.getObjectId());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ObjectContext(org.apache.cayenne.ObjectContext) 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