Skip to content

Instantly share code, notes, and snippets.

@tbml
Last active June 23, 2017 08:31
Show Gist options
  • Save tbml/b9b4af6e1d63593edd30f0fb1824006a to your computer and use it in GitHub Desktop.
Save tbml/b9b4af6e1d63593edd30f0fb1824006a to your computer and use it in GitHub Desktop.
ClosedAssociation
package org.apache.polygene.runtime.unitofwork;
import org.apache.polygene.api.association.Association;
import org.apache.polygene.api.common.Optional;
import org.apache.polygene.api.entity.EntityBuilder;
import org.apache.polygene.api.identity.HasIdentity;
import org.apache.polygene.api.property.Property;
import org.apache.polygene.api.unitofwork.UnitOfWork;
import org.apache.polygene.bootstrap.AssemblyException;
import org.apache.polygene.bootstrap.ModuleAssembly;
import org.apache.polygene.test.AbstractPolygeneTest;
import org.apache.polygene.test.EntityTestAssembler;
import org.junit.Test;
public class ClosedAssociationTest extends AbstractPolygeneTest {
@Override
public void assemble(ModuleAssembly module)
throws AssemblyException {
module.entities(EntityB.class);
module.entities(EntityA.class);
module.values(EntityB.class);
module.values(EntityA.class);
new EntityTestAssembler().assemble(module);
}
@Test
public void closedUowAssocciationAccess()
throws Exception {
UnitOfWork work = unitOfWorkFactory.newUnitOfWork();
EntityB entityB1 = work.newEntity(EntityB.class);
entityB1.prop().set("value");
EntityBuilder<EntityA> builder = work.newEntityBuilder(EntityA.class);
builder.instance().assoc().set(entityB1);
builder.instance().prop().set("value");
EntityA entityA1 = builder.newInstance();
work.complete();
work = unitOfWorkFactory.newUnitOfWork();
EntityA entityA2 = work.get(entityA1);
EntityB entityB2 = entityA2.assoc().get();
entityB2.prop().get(); // TOUCH
EntityA valueA2 = work.toValue(EntityA.class, entityA2);
EntityB valueB2 = work.toValue(EntityB.class, entityB2);
work.discard();
entityB2.prop().get(); // fails if TOUCH line is commented
valueA2.prop().get(); // ok
valueB2.prop().get(); // ok
valueA2.assoc().get(); // fails always, so there is no way how to access associated data after uow close
entityA2.assoc().get(); // also fails always
}
interface EntityB extends HasIdentity {
@Optional
Property<String> prop();
}
interface EntityA extends HasIdentity {
Association<EntityB> assoc();
@Optional
Property<String> prop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment