0
votes

There's a way to map a relationship many-to-many, using the @jointable, and avoid the automatic cascade of jointable rows?

The cascade property of the @ManyToMany annotation is for create/merge/delete the related entity (not the rows of jointable) and the insertable/updatable of joincolumns/ inversejoincolumns properties in @JoinTable is ignored when I persist/merge the owned entity of relationship.

Example: In User (Owner) x Role (mappedby) relationship, when I merge an User with the collection of Role empty, all rows of table USER_ROLE is deleted for that user.

Tks in advance.


That's my point:

For make the problem question simple, let's suppose that I have an User entity with three attributes: id, email and a set of Roles (owner, @manytomany, @jointable). I will merge an User entity that have at least one or more Roles, without find it through entityManager:

User user = new User(); //detached
user.setId(1L); // for merge
user.setEmail("[email protected]");

entityManager.merge(user);

The result in database is:

UPDATE user... /*ok!*/
delete from user_role where ... /* I'd like to avoid it! */
1
Thank you very much for your reply. I will edit my question for post the problem - Tiago
@Tiago so you try to merge your User with another user in your database? - user3252538
It's an example. If I persist an User with roles, the table user_role is populated. I'd like to avoid the cascade of jointable. - Tiago
how can you avoid it? When you use merge() it means that you make your detached entity to managed entity IF there are satisfied two conditions: identity of types and PK. So if hibernate finds these parameters and it will be identical to your new detached entity, so it simple updates it in your relationship table. :) So it is true, you need to set your PK other from your already persisted entity, when you try to merge it, hibernate try to find managed entity for it, if it does not find it, it simply creates new one in your table. - user3252538
:) Yes, but in another kind of relationship, like ManyToOne, using @JoinColumn, I can put updatable=false and I avoid the update of it. In resume, I don't wanna update the relationship as a part of entity when I persist or merge the owner entity of relationship. I didn't see a way to do it, so I post the question here for check if it make sense and if someone has an idea to do it. Do you think that avoid it doesn't make sense? In other words: Always that I persist/merge an owner entity that have manytomany relationship It will persist the relationship. Tks for your attention :) - Tiago

1 Answers

1
votes

I think, the problem is that you are trying to merge a detached entity.If So, detached entity is not anymore in the same session where you persist it. When you merge it, you create new instance and try to copy state from previous managed instance (which is not anymore managed entity because it is detached entity) so you get an empty state and hibernate deletes this object because it is empty. You need to take your managed entity from session and then do merge. This is only my guess.

EDIT:

Your entities are cascaded. The result of these entities is represented in your user_role. Let's say you persisted User (ID=30L) with some roles collection. Now, you worked with this detached object in your UI and due to some cases wanted to merge() . If so, you simply create new instance with the SAME TYPE and THE SAME PK (in your UI and set some new state for it (USUALLY you do not create it, you simply get it from session scope by id). When you try to execute it, hibernate try to find object with the same identity.

  1. If it finds it, it simply updates it in your table.

  2. If no, it creates a new one in your table.

    So i think your version is the 1st one.