Hey,

I made a quick spike to see how AssociationRoles could work. I got it 
working with the MemoryEntityStoreService without problems. Here's what 
the application code looks like:
Movie movie;
{
     UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork();
     {
         // Owning Entity
         EntityBuilder<Movie> builder = unitOfWork.newEntityBuilder( 
Movie.class );
         builder.stateOfComposite().name().set( "Telling Lies in America" );
         movie = builder.newInstance();
     }

     Actor actor;
     {
         // Associated Entity
         EntityBuilder<Actor> builder = unitOfWork.newEntityBuilder( 
Actor.class );
         builder.stateOfComposite().name().set("Kevin Bacon");
         actor = builder.newInstance();
     }

     Role role;
     {
         // The role of the Entity in the specific Association
         EntityBuilder<Role> builder = 
unitOfWork.newEntityBuilder(Role.class);
         builder.stateOfComposite().name().set("Billy Magic");
         role = builder.newInstance();
     }

     // Add the role-specified Entity to the ManyAssociation
     movie.actors().add( role( actor, role ));

     unitOfWork.complete();
}

{
     UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork();
     movie= unitOfWork.dereference( movie );

     assertThat( "movie is correct", movie.name().get(), 
equalTo("Telling Lies in America") );
     assertThat( "actor is correct", 
movie.actors().iterator().next().association().name().get(), 
equalTo("Kevin Bacon") );
     assertThat( "role is correct", 
movie.actors().iterator().next().role().name().get(), equalTo("Billy 
Magic") );

     unitOfWork.complete();
}
---
The magic is the static method "AssociationRole.role(<A>,<R>)" which 
creates an AssociationRole, which is then set as the value for the 
association instead of the EntityComposite Actor. The 
ManyAssociationInstance implementation checks for AssociationRole when 
converting the reference to a QualifiedIdentity, and instead creates an 
instance of the AssociationRoleQualifiedIdentity which has an additional 
QualifiedIdentity for the role. This assumes that the role is an Entity 
too, but I think that could be a good idea anyway. It makes it trivial 
to implement EntityStores, since all they have to do is store a 
composite key (associated entity key concatenated with role entity key). 
This puts a minimal requirement on EntityStores to support all of this. 
If the role is an Entity that also gives us great possibilities to have 
quite complex data in it, including letting it have associations which 
in turn can have roles.

The Entity interfaces for the above are like so:
interface Movie
     extends EntityComposite
{
     Property<String> name();
     ManyAssociation<AssociationRole<Actor, Role>> actors();
}

interface Role
     extends EntityComposite
{
     Property<String> name();
}

interface Actor
     extends EntityComposite
{
     Property<String> name();
}
---
i.e. trivial.

I had to update some of the internals that convert entities to qid's, 
but by and large the changes were quite minimal. Not quite sure yet how 
this will impact querying/indexing though.

What say ye?

/Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to