Many to many JPA, insert in table A and join table only if table B has entry

I have a many to many association between two tables, table A and B, and a Join table linking them:

enter image description here

Is there any way using JPA or DB Logic to post something in A and Join ONLY if B has certain entry??


If A and B are Entities, you can use @PrePersist annotation. Simply @PrePersit means Before Insert, so you can check the entries in B before inserting in A.

Class A{
int id;
String name;

@PrePersist
public void beforeInsert(){
//if B has certain entry
//then save in Join
}
}