@ -15,26 +15,38 @@
@@ -15,26 +15,38 @@
* /
package org.springframework.data.jdbc.repository ;
import static java.util.Arrays.* ;
import static org.assertj.core.api.Assertions.* ;
import org.junit.jupiter.api.Test ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.ComponentScan ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.FilterType ;
import org.springframework.context.annotation.Import ;
import org.springframework.core.convert.converter.Converter ;
import org.springframework.data.annotation.Id ;
import org.springframework.data.convert.CustomConversions ;
import org.springframework.data.convert.ReadingConverter ;
import org.springframework.data.convert.WritingConverter ;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions ;
import org.springframework.data.jdbc.core.mapping.AggregateReference ;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes ;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories ;
import org.springframework.data.jdbc.testing.DatabaseType ;
import org.springframework.data.jdbc.testing.EnabledOnDatabase ;
import org.springframework.data.jdbc.testing.IntegrationTest ;
import org.springframework.data.jdbc.testing.TestConfiguration ;
import org.springframework.data.mapping.model.SimpleTypeHolder ;
import org.springframework.data.relational.core.dialect.Dialect ;
import org.springframework.data.relational.core.mapping.RelationalMappingContext ;
import org.springframework.data.repository.CrudRepository ;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate ;
import org.springframework.test.jdbc.JdbcTestUtils ;
import java.util.Collections ;
/ * *
* Very simple use cases for creation and usage of JdbcRepositories .
*
@ -52,13 +64,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
@@ -52,13 +64,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
@Configuration
@Import ( TestConfiguration . class )
@EnableJdbcRepositories ( considerNestedRepositories = true ,
includeFilters = @ComponentScan.Filter ( value = Ones . class , type = FilterType . ASSIGNABLE_TYPE ) )
includeFilters = @ComponentScan.Filter ( value = { Ones . class , ReferencingAggregateRepository . class } , type = FilterType . ASSIGNABLE_TYPE ) )
static class Config {
@Bean
JdbcCustomConversions jdbcCustomConversions ( ) {
return new JdbcCustomConversions ( asList ( AggregateIdToLong . INSTANCE , LongToAggregateId . INSTANCE ) ) ;
}
}
@Autowired NamedParameterJdbcTemplate template ;
@Autowired Ones ones ;
@Autowired ReferencingAggregateRepository referencingAggregates ;
@Autowired RelationalMappingContext context ;
@SuppressWarnings ( "ConstantConditions" )
@ -95,6 +112,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
@@ -95,6 +112,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
) . isEqualTo ( 1 ) ;
}
@Test // DATAJDBC-221
public void savesAndReadWithConvertableId ( ) {
AggregateReference < AggregateWithConvertableId , AggregateId > idReference = AggregateReference . to ( new AggregateId ( TWO_ID ) ) ;
ReferencingAggregate reference = referencingAggregates . save ( new ReferencingAggregate ( null , "Reference" , idReference ) ) ;
ReferencingAggregate reloaded = referencingAggregates . findById ( reference . id ) . get ( ) ;
assertThat ( reloaded . id ( ) ) . isEqualTo ( idReference ) ;
}
interface Ones extends CrudRepository < AggregateOne , Long > { }
static class AggregateOne {
@ -109,4 +138,40 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
@@ -109,4 +138,40 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
@Id Long id ;
String name ;
}
interface ReferencingAggregateRepository extends CrudRepository < ReferencingAggregate , Long > {
}
record AggregateWithConvertableId ( @Id AggregateId id , String name ) {
}
record AggregateId ( Long value ) {
}
record ReferencingAggregate ( @Id Long id , String name ,
AggregateReference < AggregateWithConvertableId , AggregateId > ref ) {
}
@WritingConverter
enum AggregateIdToLong implements Converter < AggregateId , Long > {
INSTANCE ;
@Override
public Long convert ( AggregateId source ) {
return source . value ;
}
}
@ReadingConverter
enum LongToAggregateId implements Converter < Long , AggregateId > {
INSTANCE ;
@Override
public AggregateId convert ( Long source ) {
return new AggregateId ( source ) ;
}
}
}