Search in sources :

Example 1 with JdbcPersistentEntity

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity 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 2 with JdbcPersistentEntity

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method getPropertyMap.

private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersistentEntity<S> persistentEntity) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
        if (!property.isEntity()) {
            Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
            Object convertedValue = convert(value, property.getColumnType());
            parameters.addValue(property.getColumnName(), convertedValue, JdbcUtil.sqlTypeFor(property.getColumnType()));
        }
    });
    return parameters;
}
Also used : NonTransientDataAccessException(org.springframework.dao.NonTransientDataAccessException) EntityInformation(org.springframework.data.repository.core.EntityInformation) HashMap(java.util.HashMap) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) JdbcUtil(org.springframework.data.jdbc.support.JdbcUtil) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) KeyHolder(org.springframework.jdbc.support.KeyHolder) Collectors(java.util.stream.Collectors) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations) PropertyHandler(org.springframework.data.mapping.PropertyHandler) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Map(java.util.Map) BasicJdbcPersistentEntityInformation(org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation) JdbcPersistentEntity(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity) RowMapper(org.springframework.jdbc.core.RowMapper) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) PropertyPath(org.springframework.data.mapping.PropertyPath) JdbcPersistentEntityInformation(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation) JdbcMappingContext(org.springframework.data.jdbc.mapping.model.JdbcMappingContext) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Assert(org.springframework.util.Assert) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Aggregations

JdbcPersistentEntity (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)2 JdbcPersistentProperty (org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 NonTransientDataAccessException (org.springframework.dao.NonTransientDataAccessException)1 BasicJdbcPersistentEntityInformation (org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation)1 JdbcMappingContext (org.springframework.data.jdbc.mapping.model.JdbcMappingContext)1 JdbcPersistentEntityInformation (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation)1 JdbcUtil (org.springframework.data.jdbc.support.JdbcUtil)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 PropertyHandler (org.springframework.data.mapping.PropertyHandler)1 PropertyPath (org.springframework.data.mapping.PropertyPath)1 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)1 EntityInformation (org.springframework.data.repository.core.EntityInformation)1 RowMapper (org.springframework.jdbc.core.RowMapper)1