use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.
the class DefaultDataAccessStrategy method insert.
@Override
public <T> void insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {
KeyHolder holder = new GeneratedKeyHolder();
JdbcPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
JdbcPersistentEntityInformation<T, ?> entityInformation = context.getRequiredPersistentEntityInformation(domainType);
MapSqlParameterSource parameterSource = getPropertyMap(instance, persistentEntity);
Object idValue = getIdValueOrNull(instance, persistentEntity);
JdbcPersistentProperty idProperty = persistentEntity.getIdProperty();
if (idValue != null) {
additionalParameters.put(idProperty.getColumnName(), convert(idValue, idProperty.getColumnType()));
}
additionalParameters.forEach(parameterSource::addValue);
//
operations.update(//
sql(domainType).getInsert(additionalParameters.keySet()), //
parameterSource, //
holder);
setIdFromJdbc(instance, holder, persistentEntity);
// The database should have created an id and provided it.
if (idProperty != null && idValue == null && entityInformation.isNew(instance)) {
throw new IllegalStateException(String.format(ENTITY_NEW_AFTER_INSERT, persistentEntity));
}
}
use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.
the class EntityRowMapper method readEntityFrom.
private <S> S readEntityFrom(ResultSet rs, PersistentProperty<?> property) {
String prefix = property.getName() + "_";
@SuppressWarnings("unchecked") JdbcPersistentEntity<S> entity = (JdbcPersistentEntity<S>) context.getRequiredPersistentEntity(property.getActualType());
if (readFrom(rs, entity.getRequiredIdProperty(), prefix) == null) {
return null;
}
S instance = instantiator.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, prefix));
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(accessor, conversions);
for (JdbcPersistentProperty p : entity) {
propertyAccessor.setProperty(p, readFrom(rs, p, prefix));
}
return instance;
}
use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.
the class SqlGenerator method cascadeConditions.
private String cascadeConditions(String innerCondition, PropertyPath path) {
if (path == null) {
return innerCondition;
}
JdbcPersistentEntity<?> entity = context.getRequiredPersistentEntity(path.getOwningType());
JdbcPersistentProperty property = entity.getPersistentProperty(path.getSegment());
Assert.notNull(property, "could not find property for path " + path.getSegment() + " in " + entity);
return //
String.format(//
"%s IN (SELECT %s FROM %s WHERE %s)", //
property.getReverseColumnName(), //
entity.getIdColumn(), //
entity.getTableName(), //
innerCondition);
}
use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.
the class SqlGenerator method createDeleteByPath.
String createDeleteByPath(PropertyPath path) {
JdbcPersistentEntity<?> entityToDelete = context.getRequiredPersistentEntity(path.getLeafType());
JdbcPersistentEntity<?> owningEntity = context.getRequiredPersistentEntity(path.getOwningType());
JdbcPersistentProperty property = owningEntity.getRequiredPersistentProperty(path.getSegment());
String innerMostCondition = String.format("%s = :rootId", property.getReverseColumnName());
String condition = cascadeConditions(innerMostCondition, path.next());
return String.format("DELETE FROM %s WHERE %s", entityToDelete.getTableName(), condition);
}
use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.
the class SqlGenerator method addColumnsAndJoinsForOneToOneReferences.
/**
* Adds the columns to the provided {@link SelectBuilder} representing simplem properties, including those from
* one-to-one relationships.
*
* @param builder The {@link SelectBuilder} to be modified.
*/
private void addColumnsAndJoinsForOneToOneReferences(SelectBuilder builder) {
for (JdbcPersistentProperty property : entity) {
if (//
!property.isEntity() || //
Collection.class.isAssignableFrom(property.getType()) || //
Map.class.isAssignableFrom(property.getType())) {
continue;
}
JdbcPersistentEntity<?> refEntity = context.getRequiredPersistentEntity(property.getActualType());
String joinAlias = property.getName();
builder.join(jb -> //
jb.leftOuter().table(refEntity.getTableName()).as(joinAlias).where(property.getReverseColumnName()).eq().column(entity.getTableName(), entity.getIdColumn()));
for (JdbcPersistentProperty refProperty : refEntity) {
//
builder.column(cb -> //
cb.tableAlias(joinAlias).column(//
refProperty.getColumnName()).as(//
joinAlias + "_" + refProperty.getColumnName()));
}
}
}
Aggregations