diff --git a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/H2RepositorySpec.groovy b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/H2RepositorySpec.groovy index 24443df3a21..697c29d1bbd 100644 --- a/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/H2RepositorySpec.groovy +++ b/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/H2RepositorySpec.groovy @@ -16,8 +16,6 @@ package io.micronaut.data.jdbc.h2 import groovy.transform.Memoized -import io.micronaut.data.tck.entities.EntityIdClass -import io.micronaut.data.tck.entities.EntityWithIdClass import io.micronaut.data.tck.repositories.AuthorRepository import io.micronaut.data.tck.repositories.BasicTypesRepository import io.micronaut.data.tck.repositories.BookDtoRepository @@ -113,6 +111,12 @@ class H2RepositorySpec extends AbstractRepositorySpec implements H2TestPropertyP @Shared H2EntityWithIdClass2Repository entityWithIdClass2Repo = context.getBean(H2EntityWithIdClass2Repository) + @Shared + H2HeadlessBookRepository headlessBookRepository = context.getBean(H2HeadlessBookRepository) + + @Shared + H2HeadlessAuthorRepository headlessAuthorRepository = context.getBean(H2HeadlessAuthorRepository) + @Override EntityWithIdClassRepository getEntityWithIdClassRepository() { return entityWithIdClassRepo @@ -293,4 +297,32 @@ class H2RepositorySpec extends AbstractRepositorySpec implements H2TestPropertyP cleanupData() } + void "test headless repos"() { + given: + saveSampleBooks() + + when: "Test object mapping" + def author = headlessAuthorRepository.getByName("Stephen King") + + then: "The result is correct" + author != null + + when: "Find books" + def books = headlessBookRepository.findByPattern("The%") + + then: "The result is correct" + books.size() == 3 + + // TODO Could not resolve root entity. + // Either implement the Repository interface or define the entity as part of the signature + // when: "Count books" + // def count = headlessBookRepository.countBooks() + + // then: "The result is correct" + // count == 6 + + then: + cleanupData() + } + } diff --git a/data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HeadlessAuthorRepository.java b/data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HeadlessAuthorRepository.java new file mode 100644 index 00000000000..71867bb9f8e --- /dev/null +++ b/data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HeadlessAuthorRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017-2020 original 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 + * + * https://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 io.micronaut.data.jdbc.h2; + +import io.micronaut.data.annotation.Query; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; +import io.micronaut.data.tck.entities.Author; + +@JdbcRepository(dialect = Dialect.H2) +interface H2HeadlessAuthorRepository { + @Query(value = "select * from author where name = :name", nativeQuery = true) + Author getByName(String name); +} diff --git a/data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HeadlessBookRepository.java b/data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HeadlessBookRepository.java new file mode 100644 index 00000000000..7124a52a299 --- /dev/null +++ b/data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HeadlessBookRepository.java @@ -0,0 +1,33 @@ +/* + * Copyright 2017-2020 original 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 + * + * https://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 io.micronaut.data.jdbc.h2; + +import io.micronaut.data.annotation.Query; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; +import io.micronaut.data.tck.entities.Book; + +import java.util.List; + +@JdbcRepository(dialect = Dialect.H2) +public abstract class H2HeadlessBookRepository { + @Query(value = "select * from book where title like :title", nativeQuery = true) + abstract List findByPattern(String title); + + // Could not resolve root entity. Either implement the Repository interface or define the entity as part of the signature + // @Query(value = "select count(*) from book", nativeQuery = true) + // abstract Integer countBooks(); +} diff --git a/data-processor/src/main/java/io/micronaut/data/processor/visitors/RepositoryTypeElementVisitor.java b/data-processor/src/main/java/io/micronaut/data/processor/visitors/RepositoryTypeElementVisitor.java index 8e6ff37198b..48af6eca96a 100644 --- a/data-processor/src/main/java/io/micronaut/data/processor/visitors/RepositoryTypeElementVisitor.java +++ b/data-processor/src/main/java/io/micronaut/data/processor/visitors/RepositoryTypeElementVisitor.java @@ -633,7 +633,7 @@ private SourcePersistentEntity resolvePersistentEntity(MethodElement element, Ma } return entity; } else { - throw new MatchFailedException("Could not resolved root entity. Either implement the Repository interface or define the entity as part of the signature", element); + throw new MatchFailedException("Could not resolve root entity. Either implement the Repository interface or define the entity as part of the signature", element); } } diff --git a/src/main/docs/guide/shared/repositories.adoc b/src/main/docs/guide/shared/repositories.adoc index 4b9e9e00746..cda53740b62 100644 --- a/src/main/docs/guide/shared/repositories.adoc +++ b/src/main/docs/guide/shared/repositories.adoc @@ -2,7 +2,7 @@ Micronaut Data repositories are defined as interfaces that are annotated with th The ann:data.annotation.Repository[] annotation accepts an optional string value which represents the name of the connection or datasource in a multiple datasource scenario. By default, Micronaut Data will look for the default datasource. -It's possible to annotate the repository injection point with ann:data.annotation.Repository[] and set the data source name. Note that you cannot inject generic repositories, each repository needs to be bound to an entity. +It's possible to annotate the repository injection point with ann:data.annotation.Repository[] and set the data source name. Note that you can inject generic repositories not bound to an entities. The entity to treat as the root entity for the purposes of querying is established either from the method signature or from the generic type parameter specified to the api:data.repository.GenericRepository[] interface.