Deep diving into Spring JPA and I have troubles to understand whats going on here. This is the code:
Entity
@Entity
public class PersonP {
@Id @GeneratedValue
private int id;
public String name;
@ManyToMany
public List<PersonP> knows = new LinkedList<>();
@ManyToMany(mappedBy = "knows", cascade = CascadeType.ALL)
public List<PersonP> knownBy = new LinkedList<>();
public PersonP(){}
public PersonP(String name) {
this.name = name;
}
}
SpringBootApp
@SpringBootApplication
@EntityScan(basePackageClasses = Demo.class)
public class Demo implements CommandLineRunner {
@PersistenceContext
EntityManager em;
public static void main(String[] args) {
new SpringApplicationBuilder(Demo.class).run(args);
}
@Override
@Transactional
public void run(String... args) throws Exception {
sample();
}
@Transactional
public void sample(){
PersonP p1 = new PersonP("P1");
PersonP p2 = new PersonP("P2");
PersonP p3 = new PersonP("P3");
PersonP p4 = new PersonP("P4");
PersonP p5 = new PersonP("P5");
p1.knows.add(p2);
p2.knows.add(p3);
p5.knows.add(p3);
p1.knownBy.add(p3);
p3.knownBy.add(p2);
p2.knownBy.add(p4);
p1 = em.merge(p1);
p1.name = "x1";
p2.name = "x2";
p3.name = "x3";
p4.name = "x4";
p5.name = "x5";
}
By starting the app, the following gets generated in the DB:
I get to the point where all the persons are generated, but I would expect one additional entry in the PERSONP_KNOWS
table, e.g. for p2.knownBy.add(p4);
But it isnt. I assumed since calling merge
the associated entities would be saved too (p1 knows p2 and p2 is known by p4). It doesn't seem to be the case though...
Why is this so?