Browse Source
AggregateReferences can be used to reference other aggregates via their aggregate root without making them part of the referencing aggregate. I.e. the referenced entities will not be included in SQL statements for the referencing aggregates, apart from their id. Conversion between AggregateReferences and their ids and vice versa is done by the RelationalConverter.pull/94/merge
12 changed files with 382 additions and 17 deletions
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
/* |
||||
* Copyright 2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jdbc.core.mapping; |
||||
|
||||
import lombok.RequiredArgsConstructor; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
|
||||
/** |
||||
* A reference to the aggregate root of a different aggregate. |
||||
* |
||||
* @param <T> the type of the referenced aggregate root. |
||||
* @param <ID> the type of the id of the referenced aggregate root. |
||||
* |
||||
* @author Jens Schauder |
||||
* |
||||
* @since 1.0 |
||||
*/ |
||||
public interface AggregateReference<T, ID> { |
||||
|
||||
static <T, ID> AggregateReference<T, ID> to(ID id) { |
||||
return new IdOnlyAggregateReference<>(id); |
||||
} |
||||
|
||||
/** |
||||
* @return the id of the referenced aggregate. May be {@code null}. |
||||
*/ |
||||
@Nullable |
||||
ID getId(); |
||||
|
||||
/** |
||||
* An {@link AggregateReference} that only holds the id of the referenced aggregate root. |
||||
* |
||||
* Note that there is no check that a matching aggregate for this id actually exists. |
||||
* |
||||
* @param <T> |
||||
* @param <ID> |
||||
*/ |
||||
@RequiredArgsConstructor |
||||
class IdOnlyAggregateReference<T, ID> implements AggregateReference<T, ID> { |
||||
|
||||
private final ID id; |
||||
|
||||
@Override |
||||
public ID getId() { |
||||
return id; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
/* |
||||
* Copyright 2017-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jdbc.repository; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
|
||||
import org.junit.ClassRule; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.jdbc.core.mapping.AggregateReference; |
||||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; |
||||
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
||||
import org.springframework.data.jdbc.testing.TestConfiguration; |
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
||||
import org.springframework.data.repository.CrudRepository; |
||||
import org.springframework.jdbc.core.JdbcTemplate; |
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit4.rules.SpringClassRule; |
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule; |
||||
import org.springframework.test.jdbc.JdbcTestUtils; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
/** |
||||
* Very simple use cases for creation and usage of JdbcRepositories. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
@ContextConfiguration |
||||
@Transactional |
||||
public class JdbcRepositoryCrossAggregateHsqlIntegrationTests { |
||||
|
||||
private static final long TWO_ID = 23L; |
||||
|
||||
@Configuration |
||||
@Import(TestConfiguration.class) |
||||
@EnableJdbcRepositories(considerNestedRepositories = true) |
||||
static class Config { |
||||
|
||||
@Autowired JdbcRepositoryFactory factory; |
||||
|
||||
@Bean |
||||
Class<?> testClass() { |
||||
return JdbcRepositoryCrossAggregateHsqlIntegrationTests.class; |
||||
} |
||||
} |
||||
|
||||
@ClassRule public static final SpringClassRule classRule = new SpringClassRule(); |
||||
@Rule public SpringMethodRule methodRule = new SpringMethodRule(); |
||||
|
||||
@Autowired NamedParameterJdbcTemplate template; |
||||
@Autowired Ones ones; |
||||
@Autowired RelationalMappingContext context; |
||||
|
||||
@SuppressWarnings("ConstantConditions") |
||||
@Test // DATAJDBC-221
|
||||
public void savesAndRead() { |
||||
|
||||
AggregateOne one = new AggregateOne(); |
||||
one.name = "Aggregate - 1"; |
||||
one.two = AggregateReference.to(TWO_ID); |
||||
|
||||
one = ones.save(one); |
||||
|
||||
AggregateOne reloaded = ones.findById(one.id).get(); |
||||
assertThat(reloaded.two.getId()).isEqualTo(TWO_ID); |
||||
} |
||||
|
||||
@Test // DATAJDBC-221
|
||||
public void savesAndUpdate() { |
||||
|
||||
AggregateOne one = new AggregateOne(); |
||||
one.name = "Aggregate - 1"; |
||||
one.two = AggregateReference.to(42L); |
||||
one = ones.save(one); |
||||
|
||||
one.two = AggregateReference.to(TWO_ID); |
||||
|
||||
ones.save(one); |
||||
|
||||
assertThat( //
|
||||
JdbcTestUtils.countRowsInTableWhere( //
|
||||
(JdbcTemplate) template.getJdbcOperations(), //
|
||||
"aggregate_one", //
|
||||
"two = " + TWO_ID) //
|
||||
).isEqualTo(1); |
||||
} |
||||
|
||||
interface Ones extends CrudRepository<AggregateOne, Long> {} |
||||
|
||||
static class AggregateOne { |
||||
|
||||
@Id Long id; |
||||
String name; |
||||
AggregateReference<AggregateTwo, Long> two; |
||||
} |
||||
|
||||
static class AggregateTwo { |
||||
|
||||
@Id Long id; |
||||
String name; |
||||
} |
||||
} |
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.relational.core.conversion; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
|
||||
import org.assertj.core.api.SoftAssertions; |
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.ConversionService; |
||||
import org.springframework.core.convert.support.DefaultConversionService; |
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.jdbc.core.mapping.AggregateReference; |
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; |
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
||||
import org.springframework.data.util.ClassTypeInformation; |
||||
|
||||
/** |
||||
* Unit tests for the handling of {@link AggregateReference}s in the |
||||
* {@link org.springframework.data.relational.core.conversion.BasicRelationalConverter}. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class BasicRelationalConverterAggregateReferenceUnitTests { |
||||
|
||||
SoftAssertions softly = new SoftAssertions(); |
||||
|
||||
ConversionService conversionService = new DefaultConversionService(); |
||||
|
||||
RelationalMappingContext context = new RelationalMappingContext(); |
||||
RelationalConverter converter = new BasicRelationalConverter(context); |
||||
|
||||
RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(DummyEntity.class); |
||||
|
||||
@Test // DATAJDBC-221
|
||||
public void convertsToAggregateReference() { |
||||
|
||||
final RelationalPersistentProperty property = entity.getRequiredPersistentProperty("reference"); |
||||
|
||||
Object readValue = converter.readValue(23, property.getTypeInformation()); |
||||
|
||||
assertThat(readValue).isInstanceOf(AggregateReference.class); |
||||
assertThat(((AggregateReference<DummyEntity, Long>) readValue).getId()).isEqualTo(23L); |
||||
} |
||||
|
||||
@Test // DATAJDBC-221
|
||||
public void convertsFromAggregateReference() { |
||||
|
||||
final RelationalPersistentProperty property = entity.getRequiredPersistentProperty("reference"); |
||||
|
||||
AggregateReference<Object, Integer> reference = AggregateReference.to(23); |
||||
|
||||
Object writeValue = converter.writeValue(reference, ClassTypeInformation.from(property.getColumnType())); |
||||
|
||||
assertThat(writeValue).isEqualTo(23L); |
||||
} |
||||
|
||||
private static class DummyEntity { |
||||
|
||||
@Id |
||||
Long simple; |
||||
AggregateReference<DummyEntity, Long> reference; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue