[associations] Automatic bi-directional associations for play
A module that manages all your bi-directional model associations.
Description
Do you find writing code like this cumbersome and error prone?
forum1.posts.remove(post);
post.forum = forum2;
forum2.posts.add(post);
Wouldn’t it be much easier if you could just write
forum2.posts.add(post);
and the system handles all the wiring and rewiring for you? With the benefit of eliminating errors like “detached entity passed to persist” and such?
I have created a play module that does just that. Whenever you invoke one of the operations that changes a part of the association from whichever side, then the module completes this operation on all other affected parts. This includes not only the new target and its opposite reference, but it also manages to unlink a target object from its current associated object. All hassle free and safe. It works on @OneToOne
, @OneToMany
and @ManyToMany
associations.
There are no dependencies introduced in your code. The module enhancer works on all properties of your @Entity classes having a “mappedBy” attribut on the @OneToOne, @OneToMany or @ManyToMany annotation. You do not need to declare anything else. The presence of the module is sufficient.
You can check it out at https://github.com/pareis/play-associations
Distributed under Apache 2.0 License
Written by André Pareis