Kotlin interface default methods are considered query methods [DATACMNS-1223]
See original GitHub issueBen Madore opened DATACMNS-1223 and commented
If i had an interface with method:
public List<Person> findByFirstNameAndLastNameAndAgeGreaterThan(String firstName, String lastName, Int age);
Can I create an alias of this, possibly setting some default values, by using default methods? e.g.
public List<Person> findAdults(String firstName, String lastName) {
return this.findByFirstNameAndLastNameAndAgeGreaterThan(firstName, lastName, 18);
}
I’m told that this may work in 2.x (though i don’t have a Spring 5 project to try this on) but it definitely does not work in 1.11.8 as it results in org.springframework.data.mapping.PropertyReferenceException: No property Adults found for type Person!
.
If it does not work in 2.0, it seems like it would be a nice addition… if it DOES work in 2.0 it would be nice to have documented. Well, I think either way, having documentation about the expected behaviour of default methods in Repository classes would be a nice thing to have.
Affects: 2.0.2 (Kay SR2)
Issue Links:
- DATAJPA-1489 Support for custom query implementation methods with Kotlin interfaces (“is duplicated by”)
2 votes, 5 watchers
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:12
Rui Miguel Pereira Figueira commented
Kotlin now supports default methods on interfaces using a
@JvmDefault
, but it is still an experimental feature. So, it is another workaroundRui Miguel Pereira Figueira commented
As a workaround:
Basically,
KotlinUserRepository
is not an actual repository (we mark it as@NoRepositoryBean
) but delegates into our actual Spring Data repositoryInternalKotlinUserRepository
using Kotlin delegation. This way, we can put our “default” methods inKotlinUserRepository
without worring about Spring Data to try to map them into JPA queries.