diff --git a/data-jdbc/src/main/java/io/micronaut/data/jdbc/operations/DefaultJdbcRepositoryOperations.java b/data-jdbc/src/main/java/io/micronaut/data/jdbc/operations/DefaultJdbcRepositoryOperations.java index 98386e6897e..8c4678d3715 100644 --- a/data-jdbc/src/main/java/io/micronaut/data/jdbc/operations/DefaultJdbcRepositoryOperations.java +++ b/data-jdbc/src/main/java/io/micronaut/data/jdbc/operations/DefaultJdbcRepositoryOperations.java @@ -476,9 +476,9 @@ private Stream findStream(@NonNull PreparedQuery preparedQuery, } } ); - boolean onlySingleEndedJoins = joinFetchPaths.stream() + boolean onlySingleEndedJoins = joinFetchPaths.isEmpty() || joinFetchPaths.stream() .flatMap(jp -> Arrays.stream(jp.getAssociationPath())) - .anyMatch(association -> association.getKind().isSingleEnded()); + .allMatch(association -> association.getKind().isSingleEnded()); // Cannot stream ResultSet for "many" joined query if (!onlySingleEndedJoins) { try { diff --git a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresNoIdEntitySpec.groovy b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresNoIdEntitySpec.groovy new file mode 100644 index 00000000000..48fd0ce9b03 --- /dev/null +++ b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresNoIdEntitySpec.groovy @@ -0,0 +1,81 @@ +package io.micronaut.data.jdbc.postgres + +import io.micronaut.context.ApplicationContext +import io.micronaut.data.annotation.MappedEntity +import io.micronaut.data.jdbc.annotation.JdbcRepository +import io.micronaut.data.model.Pageable +import io.micronaut.data.model.Sort +import io.micronaut.data.model.query.builder.sql.Dialect +import io.micronaut.data.runtime.config.SchemaGenerate +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +import javax.inject.Inject +import java.sql.Connection + +class PostgresNoIdEntitySpec extends Specification implements PostgresTestPropertyProvider { + @AutoCleanup + @Shared + ApplicationContext applicationContext = ApplicationContext.run(getProperties()) + + @Shared + @Inject + NoIdEntityRepository noIdEntityRepository = applicationContext.getBean(NoIdEntityRepository) + + @Shared + @Inject + Connection connection = applicationContext.getBean(Connection) + + void setup() { + connection.prepareStatement(''' +drop sequence if exists "no_id_names_seq"; +drop table if exists "no_id_names"; +create sequence "no_id_names_seq" increment by 1; +create table "no_id_names" +( + "id" bigint default nextval('no_id_names_seq'::regclass) not null, + "first_name" varchar(255) , + "last_name" varchar(255) +); +''').withCloseable { it.executeUpdate() } + } + + void 'test no-id entity operations'() { + given: + when: + noIdEntityRepository.save(new NoIdEntity(firstName: "Xyz", lastName: "Abc")) + noIdEntityRepository.save(new NoIdEntity(firstName: "Qwe", lastName: "Jkl")) + def all = noIdEntityRepository.listAll() + def allPageable = noIdEntityRepository.listAll(Pageable.from(1, 1).order("firstName", Sort.Order.Direction.DESC)) + then: + all.size() == 2 + all.find { it.firstName == "Xyz" } + all.find { it.firstName == "Qwe" } + allPageable.size() == 1 + allPageable.find { it.firstName == "Qwe" } + } + + @Override + SchemaGenerate schemaGenerate() { + return SchemaGenerate.NONE + } +} + +@JdbcRepository(dialect = Dialect.POSTGRES) +interface NoIdEntityRepository { + + NoIdEntity save(NoIdEntity entity) + + List listAll() + + List listAll(Pageable pageable) + +} + +@MappedEntity("no_id_names") +class NoIdEntity { + + String firstName + String lastName +} diff --git a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresSequenceDefaultSpec.groovy b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresSequenceDefaultSpec.groovy index 52faa23ab94..612cdb8873a 100644 --- a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresSequenceDefaultSpec.groovy +++ b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresSequenceDefaultSpec.groovy @@ -15,36 +15,15 @@ */ package io.micronaut.data.jdbc.postgres -import io.micronaut.data.model.query.builder.sql.Dialect +import io.micronaut.data.runtime.config.SchemaGenerate import io.micronaut.test.extensions.spock.annotation.MicronautTest -import io.micronaut.test.support.TestPropertyProvider -import org.testcontainers.containers.PostgreSQLContainer -import spock.lang.Shared import spock.lang.Specification import javax.inject.Inject import java.sql.Connection @MicronautTest -class PostgresSequenceDefaultSpec extends Specification implements TestPropertyProvider { - - @Shared - PostgreSQLContainer postgres - - @Override - Map getProperties() { - postgres = new PostgreSQLContainer<>("postgres:10") - .withDatabaseName("test-database") - .withUsername("test") - .withPassword("test") - postgres.start() - [ - "datasources.default.url":postgres.getJdbcUrl(), - "datasources.default.username":postgres.getUsername(), - "datasources.default.password":postgres.getPassword(), - "datasources.default.dialect": Dialect.POSTGRES - ] as Map - } +class PostgresSequenceDefaultSpec extends Specification implements PostgresTestPropertyProvider { @Inject Connection connection @@ -54,6 +33,8 @@ class PostgresSequenceDefaultSpec extends Specification implements TestPropertyP void setup() { connection.prepareStatement(''' +drop sequence if exists "user_seq"; +drop table if exists "user_"; create sequence "user_seq" increment by 1; create table "user_" ( @@ -66,11 +47,6 @@ create table "user_" ''').withCloseable { it.executeUpdate( )} } - def cleanup() { - postgres.close() - } - - void "test sequence generation with default nextval"() { when: User user = userRepository.save("Fred") @@ -82,4 +58,9 @@ create table "user_" user.id != user2.id user.id == userRepository.findById(user.id).id } + + @Override + SchemaGenerate schemaGenerate() { + return SchemaGenerate.CREATE_DROP + } } diff --git a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresTestPropertyProvider.groovy b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresTestPropertyProvider.groovy index dbd6c7f8d52..6492786311c 100644 --- a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresTestPropertyProvider.groovy +++ b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresTestPropertyProvider.groovy @@ -31,7 +31,7 @@ trait PostgresTestPropertyProvider implements SharedDatabaseContainerTestPropert @Override int sharedSpecsCount() { - return 9 + return 11 } @Override diff --git a/data-r2dbc/src/main/java/io/micronaut/data/r2dbc/operations/DefaultR2dbcRepositoryOperations.java b/data-r2dbc/src/main/java/io/micronaut/data/r2dbc/operations/DefaultR2dbcRepositoryOperations.java index 29da7011667..78b508d1c52 100644 --- a/data-r2dbc/src/main/java/io/micronaut/data/r2dbc/operations/DefaultR2dbcRepositoryOperations.java +++ b/data-r2dbc/src/main/java/io/micronaut/data/r2dbc/operations/DefaultR2dbcRepositoryOperations.java @@ -662,9 +662,9 @@ public Flux findAll(@NonNull PreparedQuery preparedQuery) { } } ); - boolean onlySingleEndedJoins = joinFetchPaths.stream() + boolean onlySingleEndedJoins = joinFetchPaths.isEmpty() || joinFetchPaths.stream() .flatMap(jp -> Arrays.stream(jp.getAssociationPath())) - .anyMatch(association -> association.getKind().isSingleEnded()); + .allMatch(association -> association.getKind().isSingleEnded()); // Cannot stream ResultSet for "many" joined query if (!onlySingleEndedJoins) { SqlResultEntityTypeMapper.PushingMapper> manyReader = entityTypeMapper.readAllWithJoins();