Hello! I have an application with 2 classes, Person and Phone. The classes look like this.
@Entity
public class Person {
@Id
@GeneratedValue(generator = "ID_GENERATOR")
private Long id;
@NotNull
private String name;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "PERSON_ID", nullable = false)
private Set<Phone> phones = new HashSet<>();
…
}
@Entity
public class Phone {
@Id
@GeneratedValue(generator = "ID_GENERATOR")
private Long id;
@NotNull
private String owner;
…
}
I change the owner of a phone from a different thread and refresh the person:
Executors.newSingleThreadExecutor().submit(() → {
EntityManager em1 = emf.createEntityManager();
em1.getTransaction().begin();
em1.unwrap(Session.class).doWork(con -> {
PreparedStatement ps = con.prepareStatement(
"update PHONE set OWNER = ? where PERSON_ID = ?"
);
ps.setString(1, "John Jones");
ps.setLong(2, PERSON_ID);
ps.executeUpdate();
});
em1.getTransaction().commit();
em1.close();
return null;
}).get();
em.refresh(john2);
for (Phone phone : john2.getPhones()) {
assertEquals("John Jones", phone.getOwner()); //1
}
The assert from line //1 fails for MySQL, although the information was changed in the database. However, moving to H2 with absolutely the same code makes the assert from //1 to succeed.
Am I missing anything or could this be a bug?
I can provide the whole code if needed.
Thank you!