samedi 18 juin 2016

EntityListener not working when a child is removed by orphanRemoval

I'm having problems when using EntityListener in JPA.

The project is based on:

JPA 2.1.

EclipseLink 2.6.3

MySql 5.7.11

There is an Entity Author and Book, Author has a OneToMany relationship to Book and oprhanRemoval=true.

if I get an author of the database , I delete the reference to a book and keep the author . The book is deleted, but the listeners are not running

The code is self explanatory

@Entity
@Table(name = "ges_autor")
public class Author  implements Serializable, ItemIf {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @OneToMany(mappedBy = "author",cascade = {CascadeType.ALL},  fetch = FetchType.LAZY, orphanRemoval = true)
    //@PrivateOwned I have tried it without result
    protected List<Book> books;

//getters y setters
}

@Entity
@Table(name = "ges_book")
@EntityListeners({BookListener.class})
public class Book implements Serializable, ItemIf {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @ManyToOne
    @JoinColumn(name="autor_id")
    protected Author author;

//getters y setters
}



public class BookListener {
    @PrePersist
    private prePersist(Book libro){
         System.out.println("Listener libro persist ejecutado");
    }
    @PreUpdate
    private preUpdate(Book libro){
         System.out.println("Listener libro update ejecutado");
    }
    @PreRemove
    private preRemove(Book libro){
        System.out.println("Listener libro remove ejecutado");
    }
}

To reproduce the error:

Author autor = getFacade().find(id);
autor.getLibros().remove(libro);
getEntityManager().merge(autor);

The orphanRemoval is working and book is removed but the preremove listener is not working

Aucun commentaire:

Enregistrer un commentaire