Skip to content
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

Add note that headless repositories are possible #2926

Open
wants to merge 2 commits into
base: 4.8.x
Choose a base branch
from
Open
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 @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<Book> 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/shared/repositories.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading