Search in sources :

Example 1 with SqlGenerator

use of org.springframework.data.jdbc.core.convert.SqlGenerator in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method insert.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
	 */
@Override
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
    SqlGenerator sqlGenerator = sql(domainType);
    RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
    SqlIdentifierParameterSource parameterSource = getParameterSource(instance, persistentEntity, "", PersistentProperty::isIdProperty, getIdentifierProcessing());
    identifier.forEach((name, value, type) -> addConvertedPropertyValue(parameterSource, name, value, type));
    Object idValue = getIdValueOrNull(instance, persistentEntity);
    if (idValue != null) {
        RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
        addConvertedPropertyValue(parameterSource, idProperty, idValue, idProperty.getColumnName());
    }
    String insertSql = sqlGenerator.getInsert(new HashSet<>(parameterSource.getIdentifiers()));
    if (idValue == null) {
        return executeInsertAndReturnGeneratedId(domainType, persistentEntity, parameterSource, insertSql);
    } else {
        operations.update(insertSql, parameterSource);
        return null;
    }
}
Also used : SqlGenerator(org.springframework.data.jdbc.core.convert.SqlGenerator) PersistentProperty(org.springframework.data.mapping.PersistentProperty) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Aggregations

SqlGenerator (org.springframework.data.jdbc.core.convert.SqlGenerator)1 PersistentProperty (org.springframework.data.mapping.PersistentProperty)1 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)1