Skip to content

Instantly share code, notes, and snippets.

@sumanentc
Created October 29, 2019 05:03
Show Gist options
  • Save sumanentc/2582928ecf9343653fb0cb82794f273a to your computer and use it in GitHub Desktop.
Save sumanentc/2582928ecf9343653fb0cb82794f273a to your computer and use it in GitHub Desktop.
@Configuration
public class MyInterceptor {
@Autowired
private JpaProperties jpaProperties;
@Bean
public EmptyInterceptor hibernateInterceptor() {
return new EmptyInterceptor() {
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
}
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
return false;
}
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
return false;
}
};
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factory, DataSource dataSource, JpaProperties properties) {
Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
jpaPropertiesMap.put("hibernate.ejb.interceptor", hibernateInterceptor());
return factory.dataSource(dataSource).packages("com.example").properties(jpaPropertiesMap).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment