Skip to content

Move to Spring Data Commons' association abstraction. #938

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
wants to merge 1 commit into from
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
Expand Up @@ -183,7 +183,7 @@ public Class<?> getColumnType(RelationalPersistentProperty property) {

private Class<?> doGetColumnType(RelationalPersistentProperty property) {

if (property.isReference()) {
if (property.isAssociation()) {
return getReferenceColumnType(property);
}

Expand Down Expand Up @@ -419,23 +419,23 @@ private T populateProperties(T instance, @Nullable Object idValue) {
PersistentPropertyAccessor<T> propertyAccessor = getPropertyAccessor(entity, instance);
PreferredConstructor<T, RelationalPersistentProperty> persistenceConstructor = entity.getPersistenceConstructor();

for (RelationalPersistentProperty property : entity) {
entity.doWithAll(property -> {

if (persistenceConstructor != null && persistenceConstructor.isConstructorParameter(property)) {
continue;
return;
}

// skip absent simple properties
if (isSimpleProperty(property)) {

if (!propertyValueProvider.hasProperty(property)) {
continue;
return;
}
}

Object value = readOrLoadProperty(idValue, property);
propertyAccessor.setProperty(property, value);
}
});

return propertyAccessor.getBean();
}
Expand Down Expand Up @@ -513,7 +513,7 @@ private boolean hasInstanceValues(@Nullable Object idValue) {
for (RelationalPersistentProperty embeddedProperty : persistentEntity) {

// if the embedded contains Lists, Sets or Maps we consider it non-empty
if (embeddedProperty.isQualified() || embeddedProperty.isReference()) {
if (embeddedProperty.isQualified() || embeddedProperty.isAssociation()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.relational.core.dialect.IdGeneration;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
Expand Down Expand Up @@ -424,7 +423,7 @@ private <S, T> SqlIdentifierParameterSource getParameterSource(@Nullable S insta
PersistentPropertyAccessor<S> propertyAccessor = instance != null ? persistentEntity.getPropertyAccessor(instance)
: NoValuePropertyAccessor.instance();

persistentEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
persistentEntity.doWithAll(property -> {

if (skipProperty.test(property) || !property.isWritable()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
Expand Down Expand Up @@ -818,7 +817,7 @@ static class Columns {

private void populateColumnNameCache(RelationalPersistentEntity<?> entity, String prefix) {

entity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
entity.doWithAll(property -> {

// the referencing column of referenced entity is expected to be on the other side of the relation
if (!property.isEntity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,21 @@ public BasicJdbcPersistentProperty(Property property, PersistentEntity<?, Relati
super(property, owner, simpleTypeHolder, namingStrategy);
}

/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isAssociation()
*/
@Override
public boolean isReference() {
return AggregateReference.class.isAssignableFrom(getRawType());
public boolean isAssociation() {
return super.isAssociation() || AggregateReference.class.isAssignableFrom(getRawType());
}

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.mapping.BasicRelationalPersistentProperty#isReference()
*/
@Override
public boolean isReference() {
return isAssociation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public void detectsKeyColumnOverrideNameFromMappedCollectionAnnotation() {
assertThat(listProperty.getReverseColumnName(path)).isEqualTo(quoted("override_id"));
}

@Test // #938
void considersAggregateReferenceAnAssociation() {

RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(WithAssociations.class);
RelationalPersistentProperty property = entity.getRequiredPersistentProperty("association");

assertThat(property.isAssociation()).isTrue();
}

private PersistentPropertyPathExtension getPersistentPropertyPath(Class<?> type, String propertyName) {
PersistentPropertyPath<RelationalPersistentProperty> path = context
.findPersistentPropertyPaths(type, p -> p.getName().equals(propertyName)).getFirst()
Expand Down Expand Up @@ -149,4 +158,8 @@ private static class WithCollections {
@MappedCollection(idColumn = "override_id", keyColumn = "override_key") //
List<Integer> overrideList;
}

static class WithAssociations {
AggregateReference<Object, Long> association;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private SqlIdentifier createDerivedSqlIdentifier(String name) {
*/
@Override
protected Association<RelationalPersistentProperty> createAssociation() {
throw new UnsupportedOperationException();
return new Association<>(this, null);
}

public boolean isForceQuote() {
Expand All @@ -137,7 +137,7 @@ public void setForceQuote(boolean forceQuote) {

@Override
public boolean isEntity() {
return super.isEntity() && !isReference();
return super.isEntity() && !isAssociation();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
public interface RelationalPersistentProperty extends PersistentProperty<RelationalPersistentProperty> {

/**
* @deprecated since 2.2, in favor of {@link #isAssociation()}
* @return
*/
@Deprecated
boolean isReference();

/**
Expand Down