Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HibernateSecondaryTableFixIntegrator implements Integrator {
- private static final Logger log = LoggerFactory.getLogger(CacheInstanceActionController.class);
- @Override
- public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactoryImplementor,
- SessionFactoryServiceRegistry sessionFactoryServiceRegistry) {
- fixSqlStatementsSecondaryTable(metadata);
- }
- @Override
- public void disintegrate(SessionFactoryImplementor sessionFactoryImplementor, SessionFactoryServiceRegistry sessionFactoryServiceRegistry) {
- // nothing to do
- }
- private void fixSqlStatementsSecondaryTable(Metadata metadata) {
- for (PersistentClass entityBinding : metadata.getEntityBindings()) {
- if (isSecondaryTablePresent(entityBinding)) {
- addAnnotationsCustomSqlIfNecessary(entityBinding);
- }
- }
- }
- private void addAnnotationsCustomSqlIfNecessary(PersistentClass entityBinding) {
- List<String> secondaryTables = getSecondaryTablesOnEntity(entityBinding);
- List<String> secondaryTablesHeritage = getSecondaryTablesInInheritance(entityBinding);
- secondaryTables.stream()
- .filter(secondaryTablesHeritage::contains)
- .forEach(st -> addCustomSqlInsert(entityBinding, st));
- }
- private void addCustomSqlInsert(PersistentClass entityBinding, String secondaryTable) {
- Iterator<Join> joinIterator = entityBinding.getJoinIterator();
- while (joinIterator.hasNext()) {
- Join join = joinIterator.next();
- if (join.getTable().getName().equals(secondaryTable)) {
- String table = join.getTable().getName();
- String idColumn = findIdColumn(entityBinding);
- String propertiesUpdate = buildPropertiesUpdateString(join);
- String customSqlInsert = "Update " + table + propertiesUpdate + " WHERE " + idColumn + " = ?";
- join.setCustomSQLInsert(customSqlInsert, false, ExecuteUpdateResultCheckStyle.NONE);
- log.info("Correction de la requête SQL appliquée pour l'entity {}, sur la SecondaryTable {}", entityBinding.getClassName(), secondaryTable);
- }
- }
- }
- private String buildPropertiesUpdateString(Join join) {
- String propertiesUpdateString = " SET";
- Iterator<Property> propertyIterator = join.getPropertyIterator();
- while (propertyIterator.hasNext()) {
- Property property = propertyIterator.next();
- propertiesUpdateString = propertiesUpdateString.concat(" " + property.getName() + " = ? ,");
- }
- return StringUtils.chop(propertiesUpdateString);
- }
- private List<String> getSecondaryTablesInInheritance(PersistentClass entityBinding) {
- List<String> secondaryTableDansHeritage = new ArrayList<>();
- PersistentClass parent = entityBinding.getSuperclass();
- while (parent != null) {
- secondaryTableDansHeritage.addAll(getSecondaryTablesOnEntity(parent));
- parent = parent.getSuperclass();
- }
- return secondaryTableDansHeritage;
- }
- private List<String> getSecondaryTablesOnEntity(PersistentClass entityBinding) {
- List<String> secondaryTablesSurEntity = new ArrayList<>();
- entityBinding.getJoinIterator().forEachRemaining(join -> secondaryTablesSurEntity.add(((Join) join).getTable().getName()));
- return secondaryTablesSurEntity;
- }
- private String findIdColumn(PersistentClass entityBinding) {
- Column column = (Column) entityBinding.getIdentifierProperty().getColumnIterator().next();
- return column.getName();
- }
- private boolean isSecondaryTablePresent(PersistentClass entityClass) {
- return entityClass.getMappedClass().getAnnotation(SecondaryTable.class) != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement