Search in sources :

Example 1 with JdbcPersistentProperty

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));
    }
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder)

Example 2 with JdbcPersistentProperty

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;
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) JdbcPersistentEntity(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)

Example 3 with JdbcPersistentProperty

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);
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)

Example 4 with JdbcPersistentProperty

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);
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)

Example 5 with JdbcPersistentProperty

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()));
        }
    }
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) Map(java.util.Map)

Aggregations

JdbcPersistentProperty (org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)10 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JdbcPersistentEntity (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)2 PropertyPath (org.springframework.data.mapping.PropertyPath)2 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)2 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)2 KeyHolder (org.springframework.jdbc.support.KeyHolder)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 SqlSession (org.apache.ibatis.session.SqlSession)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 Assertions (org.assertj.core.api.Assertions)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1