Skip to content

Support AggregateReference in a Dto that contains only a single no-args constructor #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package org.springframework.data.jdbc.core.convert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.mapping.*;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.EntityInstantiator;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Spring {@link Converter} to create instances of the given DTO type from the source value handed into the conversion
* while also handling Aggregate References
*
* @author Mark Paluch
* @author Oliver Drotbohm
* @author Paul Jones
* @since 3.3
*/
public class JdbcDtoInstantiatingConverter implements Converter<Object, Object> {

private final Class<?> targetType;
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context;
private final EntityInstantiator instantiator;

/**
* Create a new {@link Converter} to instantiate DTOs.
*
* @param dtoType must not be {@literal null}.
* @param context must not be {@literal null}.
* @param instantiators must not be {@literal null}.
*/
public JdbcDtoInstantiatingConverter(Class<?> dtoType,
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context,
EntityInstantiators instantiators) {

Assert.notNull(dtoType, "DTO type must not be null");
Assert.notNull(context, "MappingContext must not be null");
Assert.notNull(instantiators, "EntityInstantiators must not be null");

this.targetType = dtoType;
this.context = context;
this.instantiator = instantiators.getInstantiatorFor(context.getRequiredPersistentEntity(dtoType));
}

@NonNull
@Override
public Object convert(Object source) {

if (targetType.isInterface()) {
return source;
}

PersistentEntity<?, ? extends PersistentProperty<?>> sourceEntity = context
.getRequiredPersistentEntity(source.getClass());
PersistentPropertyAccessor<Object> sourceAccessor = sourceEntity.getPropertyAccessor(source);
PersistentEntity<?, ? extends PersistentProperty<?>> targetEntity = context.getRequiredPersistentEntity(targetType);

@SuppressWarnings({ "rawtypes", "unchecked" })
Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {

@Override
@Nullable
public Object getParameterValue(Parameter parameter) {

String name = parameter.getName();

if (name == null) {
throw new IllegalArgumentException(String.format("Parameter %s does not have a name", parameter));
}

return sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(name));
}
});

PersistentPropertyAccessor<Object> targetAccessor = targetEntity.getPropertyAccessor(dto);
InstanceCreatorMetadata<? extends PersistentProperty<?>> creator = targetEntity.getInstanceCreatorMetadata();

targetEntity.doWithProperties((SimplePropertyHandler) property -> {

if ((creator != null) && creator.isCreatorParameter(property)) {
return;
}

targetAccessor.setProperty(property,
sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(property.getName())));
});

targetEntity.doWithAssociations((SimpleAssociationHandler) property -> {
if ((creator != null) && creator.isCreatorParameter(property.getInverse())) {
return;
}

if(property.getInverse().getType().equals(AggregateReference.class)) {
targetAccessor.setProperty(property.getInverse(),
sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(property.getInverse().getName())));

}
});

return dto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.data.jdbc.repository.query;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.DtoInstantiatingConverter;
import org.springframework.data.jdbc.core.convert.JdbcDtoInstantiatingConverter;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
Expand All @@ -33,6 +33,7 @@
* query and how to process the result in order to get the desired return type.
*
* @author Mark Paluch
* @author Paul Jones
* @since 2.0
*/
@FunctionalInterface
Expand All @@ -52,6 +53,7 @@ interface JdbcQueryExecution<T> {
* A {@link Converter} to post-process all source objects using the given {@link ResultProcessor}.
*
* @author Mark Paluch
* @author Paul Jones
* @since 2.3
*/
class ResultProcessingConverter implements Converter<Object, Object> {
Expand All @@ -63,7 +65,7 @@ class ResultProcessingConverter implements Converter<Object, Object> {
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> mappingContext,
EntityInstantiators instantiators) {
this.processor = processor;
this.converter = Lazy.of(() -> new DtoInstantiatingConverter(processor.getReturnedType().getReturnedType(),
this.converter = Lazy.of(() -> new JdbcDtoInstantiatingConverter(processor.getReturnedType().getReturnedType(),
mappingContext, instantiators));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
* @author Diego Krupitza
* @author Christopher Klein
* @author Mikhail Polivakha
* @author Paul Jones
*/
@IntegrationTest
public class JdbcRepositoryIntegrationTests {
Expand Down Expand Up @@ -1286,6 +1287,36 @@ void fetchByExampleFluentOnlyInstantOneValueAsSimple() {
assertThat(match.get().getName()).contains(two.getName());
}

@Test
void fetchDtoWithNoArgsConstructorWithAggregateReferencePopulated() {
DummyEntity entity = new DummyEntity();
entity.setRef(AggregateReference.to(20L));
entity.setName("Test Dto");
repository.save(entity);

assertThat(repository.findById(entity.idProp).orElseThrow().getRef()).isEqualTo(AggregateReference.to(20L));

DummyDto foundDto = repository.findDtoByIdProp(entity.idProp).orElseThrow();
assertThat(foundDto.getName()).isEqualTo("Test Dto");
assertThat(foundDto.getRef()).isEqualTo(AggregateReference.to(20L));

}

@Test
void fetchDtoWithAllArgsConstructorWithAggregateReferencePopulated() {
DummyEntity entity = new DummyEntity();
entity.setRef(AggregateReference.to(20L));
entity.setName("Test Dto");
repository.save(entity);

assertThat(repository.findById(entity.idProp).orElseThrow().getRef()).isEqualTo(AggregateReference.to(20L));

DummyAllArgsDto foundDto = repository.findAllArgsDtoByIdProp(entity.idProp).orElseThrow();
assertThat(foundDto.getName()).isEqualTo("Test Dto");
assertThat(foundDto.getRef()).isEqualTo(AggregateReference.to(20L));

}

@Test // GH-1405
void withDelimitedColumnTest() {

Expand Down Expand Up @@ -1426,6 +1457,9 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, Query

@Query("SELECT * FROM DUMMY_ENTITY WHERE DIRECTION = :direction")
List<DummyEntity> findByEnumType(Direction direction);

Optional<DummyDto> findDtoByIdProp(Long idProp);
Optional<DummyAllArgsDto> findAllArgsDtoByIdProp(Long idProp);
}

interface RootRepository extends ListCrudRepository<Root, Long> {
Expand Down Expand Up @@ -1834,6 +1868,43 @@ enum Direction {
LEFT, CENTER, RIGHT
}

static class DummyDto {
@Id Long idProp;
String name;
AggregateReference<DummyEntity, Long> ref;

public DummyDto() {
}

public String getName() {
return name;
}

public AggregateReference<DummyEntity, Long> getRef() {
return ref;
}
}

static class DummyAllArgsDto {
@Id Long idProp;
String name;
AggregateReference<DummyEntity, Long> ref;

public DummyAllArgsDto(Long idProp, String name, AggregateReference<DummyEntity, Long> ref) {
this.idProp = idProp;
this.name = name;
this.ref = ref;
}

public String getName() {
return name;
}

public AggregateReference<DummyEntity, Long> getRef() {
return ref;
}
}

interface DummyProjection {
String getName();
}
Expand Down
Loading