Skip to content

PropertyAccessingMethodInterceptor not able to look up property for Kotlin property with interface-inherited accessor method #3146

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

Closed
RamsesGomez opened this issue Aug 26, 2024 · 4 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@RamsesGomez
Copy link

RamsesGomez commented Aug 26, 2024

Just upgraded to spring boot 3.3.3 and I'm having a problem with Projections. I'm currently using
Spring Boot: 3.3.3
Kotlin: 2.0.20

Created a sample project to test it easily and on this one I decided to downgrade Koltin to version 1.9.25.

@Entity
@Table(name = "test")
class Entity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private val _id: Long? = null,

    val name: String
): Persistable<Long> {

    override fun getId(): Long? = _id

    override fun isNew(): Boolean = _id == null
}
interface Repository: JpaRepository<Entity, Long> {

    @Query("select e from Entity e")
    fun findAllProjections(): List<EntityProjection>
}


interface EntityProjection {
    val id: Long
    val name: String
}

For some reason with the new version. I cannot longer access the id using the EntityProjection interface. I get the error:

Invoked method is not a property accessor
java.lang.IllegalStateException: Invoked method is not a property accessor
	at org.springframework.data.projection.PropertyAccessingMethodInterceptor.invoke(PropertyAccessingMethodInterceptor.java:66)
	at org.springframework.data.projection.ProjectingMethodInterceptor.invoke(ProjectingMethodInterceptor.java:71)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
	at org.springframework.data.projection.ProxyProjectionFactory$TargetAwareMethodInterceptor.invoke(ProxyProjectionFactory.java:243)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:223)
	at jdk.proxy3/jdk.proxy3.$Proxy157.getId(Unknown Source)
	at com.example.projections.RepositoryTest.test(RepositoryTest.kt:17)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

If I force the version of data bom to 2024.0.2 everything works again and I'm able to access the id using the projection

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 26, 2024
@christophstrobl
Copy link
Member

Thank you for getting in touch. Please share the entire sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

@christophstrobl christophstrobl added the status: waiting-for-feedback We need additional information before we can continue label Aug 26, 2024
@RamsesGomez
Copy link
Author

RamsesGomez commented Aug 26, 2024

Here you have, you can use the unit test reproduce it

projections.zip

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Aug 26, 2024
@mp911de
Copy link
Member

mp911de commented Sep 2, 2024

There are a few issues.

First, your property is named _id while the projection uses id as property name.

Secondly, Kotlin's ReflectJvmMapping doesn't return a getter for the property, so it resorts to use Persistable.getId() but that isn't the method that is being invoked, it is Entity.getId().

@mp911de mp911de transferred this issue from spring-projects/spring-data-jpa Sep 2, 2024
@mp911de
Copy link
Member

mp911de commented Sep 2, 2024

Related to #3140

@mp911de mp911de assigned mp911de and unassigned christophstrobl Sep 2, 2024
@mp911de mp911de changed the title Projections stopped working after upgrading to 3.3.3 PropertyAccessingMethodInterceptor not able to look up property for Kotlin property with interface-inherited accessor method Sep 2, 2024
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Sep 2, 2024
@mp911de mp911de added this to the 3.3.4 (2024.0.4) milestone Sep 2, 2024
mp911de added a commit that referenced this issue Sep 2, 2024
We now attempt to detect property accessors for properties declared in Kotlin that do not have a Kotlin-style accessor but one that instead comes from an interface.

Also, we specialize accessor methods that are inherited from a Java superclass but override accessors in the Kotlin realm.

Closes #3140
Closes #3146
@mp911de mp911de closed this as completed in b99db0c Sep 2, 2024
mipo256 pushed a commit to mipo256/spring-data-commons that referenced this issue Sep 21, 2024
We now attempt to detect property accessors for properties declared in Kotlin that do not have a Kotlin-style accessor but one that instead comes from an interface.

Also, we specialize accessor methods that are inherited from a Java superclass but override accessors in the Kotlin realm.

Closes spring-projects#3140
Closes spring-projects#3146
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants