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> {