List of usage examples for org.hibernate.engine.jdbc.spi JdbcCoordinator getStatementPreparer
StatementPreparer getStatementPreparer();
From source file:com.blazebit.persistence.integration.hibernate.CustomTableBasedUpdateHandlerImpl.java
License:Apache License
@Override public int execute(SharedSessionContractImplementor s, QueryParameters queryParameters) { final SessionImplementor session = (SessionImplementor) s; final JdbcCoordinator jdbcCoordinator = session.getJdbcCoordinator(); Object jdbcCoordinatorProxy = Proxy.newProxyInstance(jdbcCoordinator.getClass().getClassLoader(), new Class[] { JdbcCoordinator.class }, new JdbcCoordinatorInvocationHandler(jdbcCoordinator, new DelegatingStatementPreparerImpl(jdbcCoordinator.getStatementPreparer()) { PreparedStatement statementProxy; SecondaryTableUpdateSupportingPreparedStatementInvocationHandler invocationHandler; @Override public PreparedStatement prepareStatement(String sql, boolean isCallable) { if (sql.isEmpty()) { // Return the statement proxy which collects parameters and then executes update/insert statements for secondary tables invocationHandler.prepareNext(); return statementProxy; } else { PreparedStatement insertStatement = super.prepareStatement(sql, isCallable); this.invocationHandler = new SecondaryTableUpdateSupportingPreparedStatementInvocationHandler( session, jdbcCoordinator.getStatementPreparer(), insertStatement, secondaryTableUpdates, secondaryTableInserts); this.statementProxy = (PreparedStatement) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[] { PreparedStatement.class }, invocationHandler); return statementProxy; }// w w w. j a v a2s. c o m } })); SessionImplementor sessionProxy = (SessionImplementor) Proxy.newProxyInstance( session.getClass().getClassLoader(), new Class[] { SessionImplementor.class, EventSource.class }, new Hibernate53SessionInvocationHandler(session, jdbcCoordinatorProxy)); return delegate.execute(sessionProxy, queryParameters); }
From source file:org.babyfish.hibernate.persister.UncompletedInitializedProperties.java
License:Open Source License
static void update(EntityPersisterBridge entityPersisterBridge, Object[] state, Object entity, Serializable id, Object rowId, SessionImplementor session) { FieldInterceptor fieldInterceptor = FieldInterceptionHelper.extractFieldInterceptor(entity); if (!(fieldInterceptor instanceof HibernateObjectModelScalarLoader)) { return;/* ww w.j a va 2s . c o m*/ } HibernateObjectModelScalarLoader hibernateObjectModelScalarLoader = (HibernateObjectModelScalarLoader) fieldInterceptor; if (!hibernateObjectModelScalarLoader.isIncompletelyInitialized()) { return; } ObjectModel objectModel = hibernateObjectModelScalarLoader.getObjectModel(); JPAObjectModelMetadata objectModelMetadata = objectModel.getObjectModelMetadata(); EntityPersister entityPersister = entityPersisterBridge.getEntityPersister(); String[] names = entityPersister.getPropertyNames(); Type[] types = entityPersister.getPropertyTypes(); boolean[] updateabilities = entityPersister.getPropertyUpdateability(); List<Integer> updatePropertyIndexList = new ArrayList<>(); for (int propertyIndex = 0; propertyIndex < names.length; propertyIndex++) { if (updateabilities[propertyIndex]) { Property property = objectModelMetadata.getMappingSources().get(names[propertyIndex]); if (property instanceof ScalarProperty) { ScalarProperty scalarProperty = (ScalarProperty) property; if (scalarProperty.isDeferrable()) { int propertyId = scalarProperty.getId(); if (!objectModel.isDisabled(propertyId) && !objectModel.isUnloaded(propertyId)) { updatePropertyIndexList.add(propertyIndex); } } } } } if (updatePropertyIndexList.isEmpty()) { return; } int[] updatePropertyIndexes = new int[updatePropertyIndexList.size()]; for (int i = updatePropertyIndexList.size() - 1; i >= 0; i--) { updatePropertyIndexes[i] = updatePropertyIndexList.get(i); } boolean[] tableUpdateNeeded = entityPersisterBridge.getTableUpdateNeeded(updatePropertyIndexes); boolean[][] propertyColumnUpdateable = entityPersisterBridge.getPropertyColumnUpdateable(); for (int tableIndex = 0; tableIndex < entityPersisterBridge.getTableSpan(); tableIndex++) { if (tableUpdateNeeded[tableIndex]) { StringBuilder builder = new StringBuilder(); builder.append("update ").append(entityPersisterBridge.getTableName(tableIndex)).append(" set "); boolean addComma = false; for (int propertyIndex : updatePropertyIndexes) { if (entityPersisterBridge.isPropertyOfTable(propertyIndex, tableIndex)) { String[] columnNames = entityPersisterBridge.getPropertyColumnNames(propertyIndex); for (int columnIndex = 0; columnIndex < columnNames.length; columnIndex++) { if (propertyColumnUpdateable[propertyIndex][columnIndex]) { if (addComma) { builder.append(", "); } else { addComma = true; } builder.append(columnNames[columnIndex]).append(" = ?"); } } } } if (!addComma) { continue; } builder.append(" where "); addComma = false; if (tableIndex == 0 && rowId != null) { builder.append(entityPersisterBridge.getRowIdName()).append(" = ?"); } else { for (String keyColumn : entityPersisterBridge.getKeyColumns(tableIndex)) { if (addComma) { builder.append(", "); } else { addComma = true; } builder.append(keyColumn).append(" = ?"); } } String sql = builder.toString(); JdbcCoordinator jdbcCoordinator = session.getTransactionCoordinator().getJdbcCoordinator(); PreparedStatement preparedStatement = jdbcCoordinator.getStatementPreparer().prepareStatement(sql); try { int paramIndex = 1; for (int propertyIndex : updatePropertyIndexes) { if (entityPersisterBridge.isPropertyOfTable(propertyIndex, tableIndex)) { types[propertyIndex].nullSafeSet(preparedStatement, state[propertyIndex], paramIndex, propertyColumnUpdateable[propertyIndex], session); paramIndex += ArrayHelper.countTrue(propertyColumnUpdateable[propertyIndex]); } } if (tableIndex == 0 && rowId != null) { preparedStatement.setObject(paramIndex, rowId); } else { entityPersister.getIdentifierType().nullSafeSet(preparedStatement, id != null ? id : objectModel.getScalar(objectModelMetadata.getEntityIdProperty().getId()), paramIndex, session); } preparedStatement.executeUpdate(); } catch (SQLException ex) { throw new HibernateException(ex); } finally { jdbcCoordinator.release(preparedStatement); } } } }