2

I'm trying to configure Spring JPA to update timestamp columns using the JPA auditing framework.

I think I've got it configured correctly, but whenever I create or update a row it just sets null on all the auditable fields. (note the fields are created in the database, and if I manually write a value, it will be overwritten with null).

What am I missing here? Do I need to explicitly set the last modified date etc?

Also my auditor bean isn't being triggered, I set a break point and it's never entered, which leads me to suspect I'm missing some configuration for the auditing service.

So far I have these definitions:

@Configuration
@EnableTransactionManagement
@EnableJpaAuditing(auditorAwareRef = "auditorBean")
@EnableJpaRepositories(basePackages="com.ideafactory.mvc.repositories.jpa")
public class PersistenceConfig
{...

And the auditor aware class:

@Component
public class AuditorBean implements AuditorAware<Customer> {
    private static final Logger LOGGER= LoggerFactory.getLogger(AuditorBean.class);

    private Customer currentAuditor;

    @Override
    public Customer getCurrentAuditor() {
        //  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //
        //     if (authentication == null || !authentication.isAuthenticated()) {
        //       return null;
        //     }
        //
        //     return ((MyUserDetails) authentication.getPrincipal()).getUser();
        LOGGER.debug("call AuditorAware.getCurrentAuditor(");

        return currentAuditor;
    }

    public void setCurrentAuditor(Customer currentAuditor) {
        this.currentAuditor = currentAuditor;
    }
}

And my entity configuration:

@Entity
@Table(name= "contact_us_notes")
public class ContactUsNote extends AbstractAuditable<Customer, Long> {...

========================== Updated ============================ Ok so i went back over the docs, and it seems I'd missed configuring the entity listener. So it's kind of working now.

But now my question becomes how in java configuration do I configure the listener as a default for all entities? (Similar to the way the docs recommend in orm.xml).

I added the entity listeners annotation below.

@Entity
@Table(name= "contact_us_notes")
@EntityListeners({AuditingEntityListener.class})
public class ContactUsNote extends AbstractAuditable<Customer, Long> {
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Richard G
  • 5,243
  • 11
  • 53
  • 95
  • possible duplicate of [Spring data auditing not working in my project](http://stackoverflow.com/questions/13306426/spring-data-auditing-not-working-in-my-project) – Stephan Oct 07 '14 at 14:50

1 Answers1

2

Have you create an orm.xml file in /resources/META-INF? I don't see it posted in your question.

Chris Savory
  • 2,597
  • 1
  • 17
  • 27
  • hi Chris, no I didn't have the orm.xml but see the update section, I've added the AuditingEntityListener through java configuration to the entity. It works but I would like to know if there's a way to default this annotation for all entities in the Persistence Unit? – Richard G Aug 06 '14 at 22:23
  • Why don't you want to use the orm.xml method? That is how you do it globally. Using `@EntityListeners` would be overriding it for a specific `@Entity`. – Chris Savory Aug 06 '14 at 23:42
  • 2
    Because I haven't got any xml configuration at the moment, and I can't immediately see any reason to introduce xml besides this, even my servlets are configured successfully with java config. – Richard G Aug 07 '14 at 05:11
  • 1
    @richard-g our project is the same way. orm.xml is our only xml in the whole project. There doesn't appear to be a way to globally add this listener through Java config at the moment. If you would like that I would suggest creating a new feature in JIRA that would allow `@EnableJpaAuditing` to globally add those listeners: https://jira.spring.io/browse/DATAJPA – Chris Savory Aug 07 '14 at 15:52
  • 1
    @RichardG You can default this annotation for all entities. See this answer: http://stackoverflow.com/a/26240077/363573 – Stephan Oct 07 '14 at 16:37
  • The linked answer from Stephan helped me out. Also check out this excample (linked xml + other files in the repo) https://github.com/spring-projects/spring-data-jpa-examples/blob/master/spring-data-jpa-example/src/main/resources/META-INF/orm.xml – Tarion Nov 13 '15 at 01:00