Created
October 29, 2019 05:03
-
-
Save sumanentc/2582928ecf9343653fb0cb82794f273a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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