user1345883
user1345883

Reputation: 451

Java EntityManager merge and @PrePersist

I use this piece of line to merge:

partner = em.merge(partner);

and this is in my entity:

@PrePersist
public void updateModificationDate(){
    this.lastModificationDate = new Date();
}

So as you can see, I want to keep up when the entity was last changed,

but the method never gets called,

Anybody an idea of what I could do?

Upvotes: 0

Views: 3980

Answers (3)

Gnana
Gnana

Reputation: 614

Try forcing em.flush() but I'm not very sure whether forcing a flush is a good practice.

Upvotes: 0

Slavik
Slavik

Reputation: 118

You can add to your entity the following annotation:

@EntityListeners(SaveOrUpdateDateListener.class)

And actually listener would be:

public class SaveOrUpdateDateListener {

    @PrePersist
    @PreUpdate
    public void setDate(final CertApplication entity) {
        final Date now = new Date();
        entity.setUpdated(now);
    }
}

Upvotes: 3

gkuzmin
gkuzmin

Reputation: 2474

You can read this and discover why prepersist is not called. Also I think that you can use @Version annotation to keep entity last modification date.

Upvotes: 1

Related Questions