On 6/14/11 18:22 , Frank Grimm wrote:
As for the first approach, when you say 'explicitly handle associations
properly', is there more to it than annotating associations with @Aggregated
(as Niclas pointed out)? BTW: The code I posted used an Association<B\>, but
the mailing list's web frontend took this as formatting.
To me, the notion of "deleting an entity" is very much related to the
domain model, and what the semantics are. For example, for me, in doing
Case Management where we have Case entities referring to CaseType
entities, if I delete a Case (which I know by design has no references
to it) I can just delete it, whereas if I want to "remove" a CaseType in
the application I cannot remove it at all, but can only remove it from
the case type listing, and mark it as removed. This is because I might
have old Cases that refer to it, which should still do so.
Another common case is that I will have an entity that collects other
entities. If I want to remove a collected entity I will call
"removeEntity(collectedEntity)" (or something like that) on the
collecting entity which will then remove it from its ManyAssociation and
then delete the entity in the UoW. So, the domain model explicitly knows
when and how to clean up references.
For the second approach, is there some code (tests) in Qi4j's code base that
demonstrates how to query for associations? The only way I can think of now
is registering the referenced element together with the referencing element
and the relevant Association in an update handler which then updates removed
element when unit a unit of work completes.
Here's an example from my code:
{
SelectedLabels.Data selectedLabels = QueryExpressions.templateFor(
SelectedLabels.Data.class );
Query<SelectedLabels> labelUsages = qbf.newQueryBuilder(
SelectedLabels.class ).
where(
QueryExpressions.contains(selectedLabels.selectedLabels(), label )).
newQuery( uowf.currentUnitOfWork() );
for (SelectedLabels labelUsage : labelUsages)
{
labelUsage.removeSelectedLabel( label );
}
}
I.e. if you know that you have a ManyAssociation to an entity, in this
case SelectedLabels.Data.selectedLabels(), then make a query for it
using the contains() operator. Once you have found all such references,
invoke domain methods to do the remove automatically. Do NOT under any
circumstances make this more "automated" than that, as you may then
violate domain rules. This is a convenience for doing it the right way,
rather than just changing the data "under the hood".
/Rickard
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev