Search in sources :

Example 26 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class DeleteRulesIT method testNullifyFlattenedNoReverse.

@Test
public void testNullifyFlattenedNoReverse() {
    // temporarily set delete rule to NULLIFY...
    int oldRule = changeDeleteRule(DeleteRule.NULLIFY);
    ObjRelationship reverse = unsetReverse();
    try {
        DeleteRuleFlatA a = context.newObject(DeleteRuleFlatA.class);
        DeleteRuleFlatB b = context.newObject(DeleteRuleFlatB.class);
        a.addToFlatB(b);
        context.commitChanges();
        // must go on without exceptions...
        context.deleteObjects(a);
        // assert that join is deleted
        assertJoinDeleted(a, b);
        assertEquals(PersistenceState.DELETED, a.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, b.getPersistenceState());
        context.commitChanges();
    } finally {
        changeDeleteRule(oldRule);
        restoreReverse(reverse);
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) DeleteRuleFlatA(org.apache.cayenne.testdo.relationships_delete_rules.DeleteRuleFlatA) DeleteRuleFlatB(org.apache.cayenne.testdo.relationships_delete_rules.DeleteRuleFlatB) Test(org.junit.Test)

Example 27 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class DeleteRulesIT method testDenyFlattenedNoReverse.

@Test
public void testDenyFlattenedNoReverse() {
    // temporarily set delete rule to DENY...
    int oldRule = changeDeleteRule(DeleteRule.DENY);
    ObjRelationship reverse = unsetReverse();
    try {
        DeleteRuleFlatA a = context.newObject(DeleteRuleFlatA.class);
        DeleteRuleFlatB b = context.newObject(DeleteRuleFlatB.class);
        a.addToFlatB(b);
        context.commitChanges();
        try {
            context.deleteObjects(a);
            fail("Must have thrown a deny exception..");
        } catch (DeleteDenyException ex) {
            // expected... but check further
            assertJoinNotDeleted(a, b);
        }
    } finally {
        changeDeleteRule(oldRule);
        restoreReverse(reverse);
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) DeleteDenyException(org.apache.cayenne.DeleteDenyException) DeleteRuleFlatA(org.apache.cayenne.testdo.relationships_delete_rules.DeleteRuleFlatA) DeleteRuleFlatB(org.apache.cayenne.testdo.relationships_delete_rules.DeleteRuleFlatB) Test(org.junit.Test)

Example 28 with ObjRelationship

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

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class NestedDataContextWriteIT method testCommitChangesToParentFlattened.

@Test
public void testCommitChangesToParentFlattened() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Artist childO1 = childContext.newObject(Artist.class);
    childO1.setArtistName("Master");
    final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
    childO2.setName("Detail1");
    childO2.addToArtistArray(childO1);
    ObjEntity ent = childContext.getEntityResolver().getObjEntity("ArtGroup");
    Collection<ObjRelationship> rels = ent.getDeclaredRelationships();
    for (ObjRelationship rel : rels) {
        System.out.println(rel.getName());
    }
    assertEquals(1, childO1.getGroupArray().size());
    assertEquals(1, childO2.getArtistArray().size());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
            Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
            assertNotNull(parentO1);
            assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
            ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
            assertNotNull(parentO2);
            assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
            assertEquals(1, parentO1.getGroupArray().size());
            assertEquals(1, parentO2.getArtistArray().size());
            assertTrue(parentO2.getArtistArray().contains(parentO1));
            assertTrue(parentO1.getGroupArray().contains(parentO2));
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 30 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class DeleteObjectIT method testDeleteObjectsRelationshipCollection.

@Test
public void testDeleteObjectsRelationshipCollection() throws Exception {
    createObjectsRelationshipCollectionDataSet();
    Artist artist = Cayenne.objectForPK(context, Artist.class, 1);
    List<Painting> paintings = artist.getPaintingArray();
    assertEquals(3, paintings.size());
    // create a clone to be able to inspect paintings after deletion
    List<Painting> paintingsClone = new ArrayList<Painting>(paintings);
    for (Painting object : paintingsClone) {
        assertEquals(PersistenceState.COMMITTED, object.getPersistenceState());
    }
    context.deleteObjects(paintings);
    // as Painting -> Artist has Nullify rule, relationship list has to be
    // cleaned up,
    // and no exceptions thrown on concurrent modification...
    ObjRelationship r = context.getEntityResolver().getObjEntity(Painting.class).getRelationship("toArtist");
    assertEquals(DeleteRule.NULLIFY, r.getDeleteRule());
    assertEquals(0, paintings.size());
    for (Painting object : paintingsClone) {
        assertEquals(PersistenceState.DELETED, object.getPersistenceState());
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ArrayList(java.util.ArrayList) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ObjRelationship (org.apache.cayenne.map.ObjRelationship)84 ObjEntity (org.apache.cayenne.map.ObjEntity)48 ObjAttribute (org.apache.cayenne.map.ObjAttribute)27 DbRelationship (org.apache.cayenne.map.DbRelationship)26 Test (org.junit.Test)24 DbEntity (org.apache.cayenne.map.DbEntity)18 DbAttribute (org.apache.cayenne.map.DbAttribute)15 DbJoin (org.apache.cayenne.map.DbJoin)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)12 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ObjectId (org.apache.cayenne.ObjectId)10 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)9 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)9 ArrayList (java.util.ArrayList)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 List (java.util.List)7 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)7 DataMap (org.apache.cayenne.map.DataMap)7 HashMap (java.util.HashMap)6