简体   繁体   中英

Spring Data JPA auditing not working

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;    

}

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM