question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlin interface default methods are considered query methods [DATACMNS-1223]

See original GitHub issue

Ben 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)

Reference URL: https://stackoverflow.com/questions/49190066/how-can-i-use-kotlin-default-methods-with-spring-data-repository-interfaces/49190067#49190067

Issue Links:

  • DATAJPA-1489 Support for custom query implementation methods with Kotlin interfaces (“is duplicated by”)

2 votes, 5 watchers

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:12

github_iconTop GitHub Comments

1reaction
spring-projects-issuescommented, Dec 30, 2020

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 workaround

0reactions
spring-projects-issuescommented, Dec 30, 2020

Rui Miguel Pereira Figueira commented

As a workaround:

@Repository
interface InternalKotlinUserRepository : Repository<User, String> {
  fun findById(username: String): User
}

@NoRepositoryBean
@Component
class KotlinUserRepository(repo: InternalKotlinUserRepository) : Repository<User, String> by repo {
  fun search(username: String) = findById(username)
}

Basically, KotlinUserRepository is not an actual repository (we mark it as @NoRepositoryBean) but delegates into our actual Spring Data repository InternalKotlinUserRepository using Kotlin delegation. This way, we can put our “default” methods in KotlinUserRepository without worring about Spring Data to try to map them into JPA queries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How can I use Kotlin default methods with Spring Data ...
DATACMNS-1223 - Kotlin interface default methods are considered query methods. KT-4779 - Generate default methods for implementations in ...
Read more >
Kotlin 1.4-M3: Generating Default Methods in Interfaces
The Kotlin compiler generates the DefaultImpls class with the speak method. This method contains the default implementation.
Read more >
Kotlin Interface Default Implementation — How does it work
It can hold default methods for functions and their default parameter values. This is the reason why koltin supports default methods natively.
Read more >
Interfaces | Kotlin
Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from ...
Read more >
Support compatibility mode for JVM default in Kotlin for ...
And Java code that invokes Kotlin DefaultImpls directly (it could be similar scenario with ... Rewrite invoke-direct to default methods in parent interfaces...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found