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;
}
}
Aggregations