Announcement Announcement Module
Collapse
No announcement yet.
@PreUpdate not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @PreUpdate not working

    Hi,

    I'm using Spring Data JPA 1.2.0.RELEASE with Hibernate 3.6.6 as the JPA provider.

    I have an entity with @PreUpdate and @PrePersist on a method :

    Code:
    @Entity
    @Table(name = "procedure")
    public class Procedure extends BaseEntity {
        // ...
        @Column(name = "updated")
        private Date updatedOn;
    
        public Date getUpdatedOn() {
            return updatedOn;
        }
    
        @PreUpdate
        @PrePersist
        public void preUpdateAndPersist() {
            this.updatedOn = new Date();
        }
    
    }
    The problem is that the preUpdateAndPersist() is called on persist but is never called on merge !
    Here's a snippet of code to test that :

    Code:
        @Test
        public void testSetUpdatedWhenPersist() {
            Procedure procedure = new Procedure();
            procedure.setProcedureReference("ref1");
            procedure = repository.save(procedure);
            assertNotNull(procedure.getUpdatedOn());
        }
    
        @Test
        @DatabaseSetup("/ec/ep/europarl/coordleg/access/interfaces/ProcedureRepositoryTest/testFindByCategoryId.xml")
        public void testSetUpdatedWhenMerge() {
            Procedure procedure = new Procedure();
            procedure.setId(1L);
            procedure.setProcedureReference("proc1");
            procedure = repository.save(procedure);
            assertNotNull(procedure.getUpdatedOn());
        }
    The second one pass through the SimpleJpaRepository.save() method, it does call merge() on the entity manager, but doesn't call the method annotated by @PreUpdate.

    Code:
    junit.framework.AssertionFailedError
    	at foo.access.interfaces.ProcedureRepositoryTest.testSetUpdatedWhenMerge(ProcedureRepositoryTest.java:104)
    ...
    Can someone enlighten me on this one ?

    Regards.

  • #2
    Exacty the same problem here. Just with a different Hibernate version (4.1.6).

    I inquired it a little bit and found the reason: the @PreUpdate and @PostUpdate method receive another instance of the entity (you can see it from debugger, ids are different) while pre persist receive the actual entity. Same thing happens in @Pre[Post]Remove methods.

    bdusauso, your methods are actually called, but on a different object, this is why you don't see any change in the persisted entity.

    Now, my question to the Spring team is: Is this the expected behaviour or is it a bug? How could it be overcome?

    Many thanks,
    Stefano

    Comment

    Working...
    X