Merge pull request #9083 from amit2103/JAVA-1192

Java 1192
This commit is contained in:
Josh Cummings
2020-04-17 10:39:01 -06:00
committed by GitHub
19 changed files with 313 additions and 7 deletions
@@ -0,0 +1,19 @@
package com.baeldung.hibernate.audit;
import java.util.Optional;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
public class AuditorAwareImpl implements AuditorAware<String> {
@Override
public String getCurrentAuditor() {
return Optional.ofNullable(SecurityContextHolder.getContext())
.map(e -> e.getAuthentication())
.map(Authentication::getName)
.orElse(null);
}
}
@@ -23,6 +23,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.baeldung.hibernate.audit.AuditorAwareImpl;
import com.baeldung.persistence.dao.IBarAuditableDao;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.IFooAuditableDao;
@@ -46,7 +47,7 @@ import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "com.baeldung.persistence" })
public class PersistenceConfig {
@@ -58,6 +59,11 @@ public class PersistenceConfig {
super();
}
@Bean("auditorProvider")
public AuditorAwareImpl auditorAwareImpl() {
return new AuditorAwareImpl();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();