This kind of a problem belongs to upper layer than mapping. What you can/should do is to search for existing object LocatedPage
with the provided url. If found - use it, if not - create new. We are doing that stuff on upper layers (Service, Business) during the incoming data binding.
There are few points I wanted you to be aware of:
1) Chapter 24. Best Practices says:
Good usecases for a real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, we think that most associations are one-to-many and many-to-one, you should be careful when using any other association style and ask yourself if it is really neccessary.
Please, think about it. We are not using many-to-many at all. Having the middle object as a full entity could bring lot of advantages (e.g. searching based on subqueries)
2) Cascade
Be careful when using the Cascade on many-to-many. This is not a setting for a middle/pairing table. It is setting for the second-end Entity. So Once your LocatedPageMap
is deleted all the ScansFoundWith
items (Scan
) will be deleted as well. Usually ... cascade is not what you want to set on many-to-many. The pairing table is "cascaded always"