To clarify: The fundamental purpose of ManyToMany, like OneToMany, is that rather than dealing with "children" of an entity as they are in the database at a given moment, instead, they should have similar semantics to a MappedField: You load it from the database, modify it to your hearts content, and then decide whether you want to save it or not. For example, calling set() on a MappedField does not write immediately to the database, and it's a good thing too--imagine a multi-page form where you can go back and forth to different screens, or a single page form that uses submit buttons that perform some action but you stay on the same page. You don't want the user's changes to go the database unless he clicks "Save." So too, one often wants to edit a list on one screen. There should be submit buttons to add records, etc., but if you don't click save it shouldn't go to the database.
So MappedOneToMany and MappedManyToMany act as collections, internally keeping track of which records were inserted or removed, but not performing a create or delete until the "field" is saved. So in other words they act as multi-valued fields.
There are two ways MappedManyToMany can do this. It can hold a list of join table records, or child table records. Either way it will have to look up the other at times.
Now in order for saves and deletes on the ManyToMany Mapper to be propagated to the MappedManyToMany fields, it has to have a list of them. The list only gets populated when code references the field, causing it to be initialized and add itself to the list of m-n fields.
So we make all of these proposed changes?
-------------------------------------
> 1. That can only happen if both sides of the relationship use
> MappedManyToMany. Is there some way to enforce this? I was thinking of using
> a combination of (a) requiring the foreign MetaMapper to extends ManyToMany,
> and (b) when a MappedManyToMany is initialized, it should check that the
> foreign MetaMapper/ManyToMany actually contains a MappedManyToMany referring
> to the current MappedManyToMany. (a) is not sufficient without (b), and (b)
> has the same problem as #2 below, that objects are lazy.
>
>
I think you're right here. Both sides have to have the mapping.. however I
don't think there is a good clean way to detect this without a compiler
plugin of some kind.
>
> 1. There is a basic problem, which is that since objects are lazy, if