goldmansachs/reladomo

How To Question: Many-to-many example

Closed this issue · 2 comments

Is anyone aware of an example of many-to-many relationship insertion? Given a relationship definition similar to this or as defined in AuditedUser.xml and joined through junction table AuditedUserGroup.xml, I would expect something like this to work:

//Create a new user who is a member of pre-existing groups
AuditedGroupList groups = ;//A list of AuditedGroups
AuditedUser user = ; // A single user

// ...user instantiation and property setting...
user.setGroups(groups);
user.cascadeInsert();

The result I would expect here is one inserted row in the AuditedUserGroup table, per group.

When I'm doing something similar however, the cascadeInsert doesn't seem to create the desired entries.

Can someone point me in the right direction here? Happy to submit a PR to update documentation once we clarify.

You don't have to wire the object graph together to get relationships to work in Reladomo.
The simplest approach is just to insert whatever you want -- relationships will just work.

In case of many-to-many, you can insert the entries into the middle table.

If you want to do cascadeInsert, you have to mark the middle table as a dependent, set those and then cascade insert.

Excellent that's very helpful - many thanks