List of usage examples for org.hibernate.persister.entity EntityPersister getPropertyUpdateability
boolean[] getPropertyUpdateability();
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;/* www . j ava2s. 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); } } } }
From source file:org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor.java
License:Apache License
@Override protected Serializable performSaveOrReplicate(Object entity, EntityKey key, EntityPersister persister, boolean useIdentityColumn, Object anything, EventSource source, boolean requiresImmediateIdAccess) { validate(entity, persister, source); Serializable id = key == null ? null : key.getIdentifier(); boolean inTxn = source.getJDBCContext().isTransactionInProgress(); boolean shouldDelayIdentityInserts = !inTxn && !requiresImmediateIdAccess; // Put a placeholder in entries, so we don't recurse back and try to save() the // same object again. QUESTION: should this be done before onSave() is called? // likewise, should it be done before onUpdate()? source.getPersistenceContext().addEntry(entity, Status.SAVING, null, null, id, null, LockMode.WRITE, useIdentityColumn, persister, false, false); cascadeBeforeSave(source, persister, entity, anything); if (useIdentityColumn && !shouldDelayIdentityInserts) { log.trace("executing insertions"); source.getActionQueue().executeInserts(); }//from w w w . jav a2 s. co m Object[] values = persister.getPropertyValuesToInsert(entity, getMergeMap(anything), source); Type[] types = persister.getPropertyTypes(); boolean substitute = substituteValuesIfNecessary(entity, id, values, persister, source); if (persister.hasCollections()) { substitute = substitute || visitCollectionsBeforeSave(entity, id, values, types, source); } if (substitute) { persister.setPropertyValues(entity, values, source.getEntityMode()); } TypeHelper.deepCopy(values, types, persister.getPropertyUpdateability(), values, source); new ForeignKeys.Nullifier(entity, false, useIdentityColumn, source).nullifyTransientReferences(values, types); new Nullability(source).checkNullability(values, persister, false); if (useIdentityColumn) { EntityIdentityInsertAction insert = new EntityIdentityInsertAction(values, entity, persister, source, shouldDelayIdentityInserts); if (!shouldDelayIdentityInserts) { log.debug("executing identity-insert immediately"); source.getActionQueue().execute(insert); id = insert.getGeneratedId(); if (id != null) { // As of HHH-3904, if the id is null the operation was vetoed so we bail key = new EntityKey(id, persister, source.getEntityMode()); source.getPersistenceContext().checkUniqueness(key, entity); } } else { log.debug("delaying identity-insert due to no transaction in progress"); source.getActionQueue().addAction(insert); key = insert.getDelayedEntityKey(); } } if (key != null) { Object version = Versioning.getVersion(values, persister); source.getPersistenceContext().addEntity(entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), values, key, version, LockMode.WRITE, useIdentityColumn, persister, isVersionIncrementDisabled(), false); //source.getPersistenceContext().removeNonExist(new EntityKey(id, persister, source.getEntityMode())); if (!useIdentityColumn) { source.getActionQueue() .addAction(new EntityInsertAction(id, values, entity, version, persister, source)); } cascadeAfterSave(source, persister, entity, anything); // Very unfortunate code, but markInterceptorDirty is private. Once HHH-3904 is resolved remove this overridden method! if (markInterceptorDirtyMethod != null) { ReflectionUtils.invokeMethod(markInterceptorDirtyMethod, this, entity, persister, source); } } return id; }
From source file:owldb.util.OWLDBEventListener.java
License:Open Source License
/** * Reassociate an Object to the Session provided. * /*from w w w . j a va 2s. co m*/ * @param source The session implementor source * @param object Worked object * @param result The loaded object * @param status The Hibernate status */ private void reassociateObject(final SessionImplementor source, final Object object, final Object result, final Status status) { final Serializable id = result instanceof OWLObject ? this.getID(source, (OWLObject) result) : source.getContextEntityIdentifier(result); if (id == null) return; final EntityPersister persister = source.getEntityPersister(null, object); final EntityMode entityMode = source.getEntityMode(); final EntityKey key = new EntityKey(id, persister, entityMode); // Get a snapshot final Type[] types = persister.getPropertyTypes(); final Object[] resultValues = persister.getPropertyValues(result, entityMode); final Object[] values = persister.getPropertyValues(object, entityMode); if (persister.hasCollections()) { for (int i = 0; i < types.length; i++) { if (!types[i].isCollectionType()) continue; if (values[i] instanceof Collection<?>) { final Collection<?> unsavedCol = (Collection<?>) values[i]; final CollectionType collectionType = (CollectionType) types[i]; final CollectionPersister colPersister = source.getFactory() .getCollectionPersister(collectionType.getRole()); final PersistenceContext persistenceContext = source.getPersistenceContext(); final PersistentCollection persistentCollection = collectionType.wrap(source, unsavedCol); final PersistentCollection resultCol = (PersistentCollection) resultValues[i]; final Serializable currentKey = resultCol.getKey(); persistenceContext.addInitializedCollection(colPersister, persistentCollection, currentKey); values[i] = persistentCollection; } persister.setPropertyValues(object, values, entityMode); } } TypeHelper.deepCopy(values, types, persister.getPropertyUpdateability(), values, source); final Object version = Versioning.getVersion(values, persister); // lazyPropertiesAreUnfetched: Will be ignored, using the existing Entry // instead source.getPersistenceContext().addEntity(object, status, values, key, version, LockMode.NONE, true, persister, false, true); persister.afterReassociate(object, source); }