-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support for Query by Example [DATAJPA-218] #572
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
Comments
Oliver Drotbohm commented What is the reason you're not using the compound key features of JPA for that. You could introduce a dedicated key class (which you already have actually) @Embeddable
class FooId {
Long bar;
Long foo;
} And then use that in your entity: @Entity
class Foo {
@EmbeddedId FooId id;
} You could then declare your repository as follows and would get the interface FooRepository extends CrudRepository<Foo, FooId> { … } |
Joachim Sauer commented Actually we do that and in most of the cases it's sufficient. But we also occasionally return DTOs directly from the repository, for example if we only needs some parts of the entity (to avoid loading the entire entity if only 5 fields are needed). Since the frontend code never directly interacts with our entities, it provides the parameter as a (by the way, thanks for fixing my markup) |
Joachim Sauer commented (see comment above) |
Thomas Darimont commented Hello, I wonder whether this could be solved in a generic way by providing support for findByExample(exampleObject) / findOneByExample(exampleObject) Query-by-Example Methods? I think of something like: The Spring Data JPA SQL Generator could generate an appropriate where-condition for every non-null (?) One could also define ad-hoc example queries like: ... someRepository.findByExample(new Example(){ Best regards, |
Thomas Darimont commented I eventually gave this a spin - please see the attached PR. Would be great if we could align those efforts with the stuff from DATAMONGO-1245 |
Joachim Sauer opened DATAJPA-218 and commented
We have a situation where (due to mapping to a legacy system) we occasionally find ourself using composite keys. Those keys are represented by dedicated ID DTOs (for example a
FooIdDTO
with thelong
propertiesbarId
andquuxId
). Thus our repositories often contain methods that likefindFooById(FooIdDTO id)
. While we do need to specify the query itself, it would be great if the mapping of the actualbarId
andquuxId
values to the named parameters could be done in some declarative way instead of having to move the entire method implementation to a custom repository implementation. An alternative use case for this would be a bean that encapsulates a DB-based search: it's properties would "naturally" map onto the defined query, but it would still need to be written explicitly in a custom implementation.I suspect that this feature would require support from Spring Data Common, but since our primary use case is in Spring Data JPA, I'm reporting it here
Issue Links:
("depends on")
("is duplicated by")
Referenced from: pull request #164
The text was updated successfully, but these errors were encountered: