-1

I am new to hibernate. I want to know if any crud operation happens so I decided to use jpa callback annotations. The problem is any of those @PrePersist @PostPersist @PreRemove @PostRemove not being called when I run the project and use UI components to perform delete & add operations. I use primefaces datatable so delete operation bounded to a ManagedBean -> MessageService ->MessageDAO. IF I only execute the main file to test it it works perfectly

MessageDAO:

@Component
public class MessageDAO {


@PersistenceContext
private EntityManager em;

@Transactional
public void register(Message message) {
    em.persist(message);
}


@Transactional
public void delete(Integer id) {

     Message m =  em.find(Message.class, id);
     em.remove(em.merge(m));
   }
}

MessageListener

public class MessageListener {

@PrePersist
public void prePersist(Message o) {
    System.out.println("Pre-Persistiting operation: " );
}

@PostPersist
public void postPersist(Message o) {
    System.out.println("Post-Persist operation: " );
}

@PreRemove
public void preRemove(Message o) {
    System.out.println("Pre-Removing operation: " );
}

@PostRemove
public void postRemove(Message o) {
    System.out.println("Post-Remove operation: " );
}

@PreUpdate
public void preUpdate(Message o) {
    System.out.println("Pre-Updating operation: ");
}

@PostUpdate
public void postUpdate(Message o) {
    System.out.println("Post-Update operation: " );
  }
}

Message

  @EntityListeners(MessageListener.class)
  @Entity
  @Table(name = "messages")
public class Message implements Serializable  {

private Integer messageId;

private String subject;

private String content;

public Message(){}

public Message(Integer messageId, String subject, String content) {
    this.messageId = messageId;
    this.subject = subject;
    this.content = content;
}


@Id
@GeneratedValue
@Column(name = "MESSAGE_ID")
public Integer getMessageId() {
    return messageId;
}

//getter setter
@PrePersist
public void prePersist() {
    System.out.println("OLDUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU!!!!!!!!!!!!");
}

}

2 Answers2

4

As per the JPA spec, JPA callbacks/listeners are not called when using JPQL BULK DELETE. They are only called when using the JPA API (em.remove). Similarly the cache and managed entity objects do not reflect such a JPQL BULK DELETE call.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • I have changed my delete method into : @Transactional public void delete(Integer id) { Message m = em.find(Message.class, id); em.remove(m); } When I call delete in main method the callbacks are called. But when I run the project and click on delete button it does not work. Is there a way to do it * – Didem Küçükkaraaslan Aug 02 '17 at 07:08
  • 1
    No idea what is your "delete" button. The JPA API will call callbacks when you use `em.remove` and if a JPA Provider doesn't then it is a bug in the JPA Provider. – Neil Stockton Aug 02 '17 at 08:06
  • It is a primefaces commandbutton bounded to a ManagedBean. In managed bean I only call MessageService.delete method which calls the messageDAO.delete – Didem Küçükkaraaslan Aug 02 '17 at 08:11
  • 1
    since you have now changed your question from the original question (i.e deleted the use of JPQL BULK DELETE) then it makes little sense continuing here. This answer relates to the original question. As said, if you have a problem with "em.remove" not calling the callbacks then you DEBUG it, and raise a bug on your JPA provider – Neil Stockton Aug 02 '17 at 08:14
0

change Your code by following example hope it will work

@Transactional
    public void delete(Long id) {

         Message m = em.find(Message.class, id);
         em.remove(em.merge(m));
       }
    }
AssenKhan
  • 576
  • 5
  • 15