JPA: Implicit cascades for relationships displayed as @ManyToMany @JoinTable?

I have the following mapping:

@Entity @Table(name = "Prequalifications") public class Prequalification implements Serializable { ... @ManyToMany @JoinTable(name = "Partnerships", joinColumns = @JoinColumn(name = "prequalification_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "company_id", referencedColumnName = "id")) private Set<Company> companies; ... } 

In the @ManyToMany + @JoinTable matching relationship, is it not implicit that the association objects (here Partnerships ) are automatically saved, deleted, etc., although

by default, relationships have an empty cascade set

? The above quote was taken from Mike Keith's β€œPro JPA 2”.

Performance

 em.merge(prequalification); 

for the specified object performs , maintaining the associated partnerships without the specified types of cascades.

Is it right that this implicit cascade has ? This is not mentioned anywhere where I looked ...

+4
source share
1 answer

Rows in the join table will be inserted / deleted as part of its object (if the bidirectional side is without MappedBy). Therefore, if you save or delete or update the pre-qualification, the rows of the connection table will also be inserted or deleted.

Objects of the target company will not be cascaded. Therefore, on remove () they will not be deleted if the list is updated, they will not be deleted if orphanRemovla is not set. Persist should also not be cascaded, but what happens when you have references to "separate" objects is a gray area. Technically, an error should be chosen because the object is new and the relationship was not a cascade. It may also try to insert and receive a constraint error. It should not cascade persistence, although your object model is technically in an invalid state, so what happens may depend on the vendor.

+13
source

Source: https://habr.com/ru/post/1401988/


All Articles