Bug 101 : Saving (insert) a bean with a ManyToMany ... does not insert into the intersection table
Priority 
High
Reported Version 
 
Logged By 
Rob
Status 
Fixed
Fixed Version 
1.2.0
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
02/04/2009
Updated 
02/04/2009
Type 
Bug
 
Attachments 
No attachments

- When you a Foo bean with a ManyToMany
- save a new Foo (becomes an insert)
- the ManyToMany properties would not not be inserted into the intersection table

There is a warning that is logged:

"Save cascade on ManyToMany [xxx]. The collection [xxx] was not a BeanCollection. The additions and removals can not be determined and *NO* inserts or deletes to the intersection table occured."

 
Rob 02 Apr 10:55
Workaround

The workaround was to fetch the bean and then make your additions to the manyToMany property and then save again.

Rob 02 Apr 10:58
Changing behaviour to:

Now, when you INSERT (not update) then any objects in a manyToMany will be considered a new association and result in an insert into the intersection table.


AKA:
If you are inserting a new Foo (not update)
... then Ebean should treat every entry in the ManyToMany Set as a new association (and insert into the intersection table).

Once ebean knows Foo is being inserted, anything in the ManyToMany *MUST* be a new association.

For an update of Foo, listening on BeanSet/List/Map detects the changes (adds/removes)... but for insert of Foo this should not be needed.

Rob 02 Apr 11:01
example

MM1Right r1 = new MM1Right();
Ebean.save(r1);

MM1Right r2 = new MM1Right();
Ebean.save(r2);

MM1Left l1 = new MM1Left();
l1.setRights(new HashSet());
l1.getRights().add(r1);
l1.getRights().add(r2);

Ebean.save(l1);

Rob 02 Apr 11:07
Can't insert and then update though

Note that you can't:
- insert l1 (bean with ManyToMany property)
- add/remove elements from its ManyToMany property ("rights")
- save l1 (update)

When you do an UPDATE on a bean with ManyToMany property Ebean is going to ignore (log a warning) the manyToMany properties in terms of inserting/deleting from the intersection table.

That is, for UPDATE Ebean really wants the Ebean specific BeanCollections (Set/List/Map) that will listen for elements added to and remove from them... which then become inserts into or deletes from the intersection table.

Note: You don't have these issues with OneToMany's ... just ManyToMany's need to listen for additions/deletions of elements.

Rob 03 Apr 07:03
Fixed in HEAD

Fixed in HEAD.

woResponse

Upload a file