I'm having trouble running the listeners correctly in JPA.
The project uses:
JPA 2.1.
EclipseLink 2.6.3
MySql 5.7.11
I have an author entity marked with the annotation OneToMany and orphanRemoval = true to the entity Book, when in the author entity I delete the reference to a book and try to update it, no listener of the book class is running.
I add the code so you can see what it is about.
@Entity
@Table(name = "ges_autor")
public class Autor implements Serializable, ItemIf {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@OneToMany(mappedBy = "autor",cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, orphanRemoval = true)
//@PrivateOwned he probado poniendola y tampoco funciona
protected List<Libros> libros;
//getters y setters
}
@Entity
@Table(name = "ges_libro")
@EntityListeners({LibroListener.class})
public class Libro implements Serializable, ItemIf {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name="autor_id")
protected Autor autor;
//getters y setters
}
public class LibroListener {
@PrePersist
private prePersist(Libro libro){
System.out.println("Listener libro persist ejecutado");
}
@PreUpdate
private preUpdate(Libro libro){
System.out.println("Listener libro update ejecutado");
}
@PreRemove
private preRemove(Libro libro){
System.out.println("Listener libro remove ejecutado");
}
}
To reproduce the error, simply load an author of the database, delete a book and make a merge.
Autor autor = getFacade().find(id);
autor.getLibros().remove(libro);
getFacade().merge(autor);
The orphanRemoval annotation works and the book is deleted from the database but no listener is running.