Reputation: 577
Folks,
We have really hard time in integrating auditing support in Spring DATA JPA.
We are using spring 3.2.11 and hibernate 4.3.0. (We don't want to use ORM.xml)
Problem is we are not receiving any event in the 'AuditAwareImpl' class when customer entity is saved.
(We debugged AuditingEntityListener and found AuditHandler is getting injected properly but at later point event is not fired. We suspect some class loading issue)
CODE below.
Basically we have two business entity.
@Entity(name = "CUSTOMER")
@Table(name = "CUSTOMER_DETAILS")
@EntityListeners(AuditingEntityListener.class)
public class Customer extends AbstractAuditable<User, Long> {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User Entity :
@Entity(name = "USER")
@Table(name = "USER_DETAILS")
public class User extends AbstractPersistable<Long>{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
We have one helper class to configure spring.
@Configuration
@EnableTransactionManagement
@EnableJpaAuditing(auditorAwareRef = "auditorAwareImpl")
@EnableJpaRepositories(basePackages = "businessclass")
public class PersistenceContext {
}
AuditAwareImpl.
@Component
public class AuditorAwareImpl implements AuditorAware<User> {
public User getCurrentAuditor() {
ApplicationContext context = ApplicationContextManager.getContext();
UserRepository repository = (UserRepository)context.getBean("userRepository");
User user = new User();
user.setName("work now");
repository.save(user);
return user;
}
}
Upvotes: 0
Views: 7621
Reputation: 21
You need to call the repository's saveAndFlush method. The save method of org.springframework.data.jpa.repository.JpaRepository supports both save and update. If it is update, it calls em.merge (), but it does not trigger the PreUpdate event. If you want to trigger PreUpdate, you Need to call the saveAndFlush method
Upvotes: 1