Skip to content

When have field with type LocalDateTime in Projection, get Projection type must be an interface! [DATACMNS-1847] #2260

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
spring-projects-issues opened this issue Dec 17, 2020 · 12 comments
Assignees
Labels
type: bug A general bug

Comments

@spring-projects-issues
Copy link

Aiksandr opened DATACMNS-1847 and commented

In Spring boot 2.4.1 & 2.4.0
When use projection with field typed LocalDateTime
example
public interface Report { UUID getId(); LocalDateTime getCreateDate(); }
after fetch it from DB, get exception when try access to LocalDateTime field getCreateDate()

 

i made sample project, if You run it like usual Spring app, that will throw this exception
https://github.com/Aiksandr/spring-jpa-demo

{{Caused by: java.lang.IllegalArgumentException: Projection type must be an interface!
at org.springframework.util.Assert.isTrue(Assert.java:121)
at org.springframework.data.projection.ProxyProjectionFactory.createProjection(ProxyProjectionFactory.java:105)
at org.springframework.data.projection.SpelAwareProxyProjectionFactory.createProjection(SpelAwareProxyProjectionFactory.java:45)
at org.springframework.data.projection.ProjectingMethodInterceptor.getProjection(ProjectingMethodInterceptor.java:160)
at org.springframework.data.projection.ProjectingMethodInterceptor.potentiallyConvertResult(ProjectingMethodInterceptor.java:108)
at org.springframework.data.projection.ProjectingMethodInterceptor.invoke(ProjectingMethodInterceptor.java:85)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)}}

And in previews versions it works ok

 

This issue already was reproduced in spring boot issue spring-projects/spring-boot#24528)


Reference URL: spring-projects/spring-boot#24528

Issue Links:

  • DATACMNS-1836 Native projection stopped working with Java8 datetime
    ("duplicates")

0 votes, 6 watchers

@idkw
Copy link

idkw commented Jan 20, 2021

Any news ? This is a breaking change for us too.

@idkw
Copy link

idkw commented Jan 20, 2021

@christophstrobl Since you worked on that issue
This issue is a duplicate of #2223, should be fixed in spring boot 2.4.3 then ?
Thanks !

@christophstrobl
Copy link
Member

Fixed via #479 included in spring-data 2.4.3, picked up by Spring Boot 2.4.2

@ajobra76
Copy link

ajobra76 commented Feb 5, 2021

Since the ObjectToObjectConverter is removed one workaround whould be to wite a own ConverterService and use a @value Annotation with Spel Expression like
@value("#{@entityToDtoConvertingHelperService.convertObjectToObject(target)}") in the Projection interface

@sgflt
Copy link

sgflt commented Feb 19, 2021

As previous comment says, conversion of custom objects is now broken.

interface X {
  MyObject getMyObject();
}

with standard javax.persistence.AttributeConverter<MyObject, String>,

Throws projection type must be an interface! during call of X.getMyObject().

@mp911de
Copy link
Member

mp911de commented Feb 19, 2021

Complex object conversion was never subject to the conversion service used for projections. The conversion service can adjust for slight conversions (long to int) but in no case for String to complex object conversion.

The underlying entity is what dictates the type that needs to be used in your projection. Alternatively, use SpEL through @Value.

@sgflt
Copy link

sgflt commented Feb 19, 2021

Umm, the code worked in Spring Boot 2.2.10, so it was jut unintended "side effect" and we are supposed to Use @Value Annotation? I really liked the previous behavior that magically used JPA converter.

Or did own conversion?

@sgflt
Copy link

sgflt commented Feb 19, 2021

I have to clarify, the projection is used in native query.

@mp911de
Copy link
Member

mp911de commented Feb 19, 2021

Feel free to file a ticket directly against Spring Data JPA along with a minimal reproducible sample so we can specifically look into that issue. JPA typically returns the appropriate result and with a sample we can trace down the cause.

@NicklasWallgren
Copy link

We're still seeing this issue while trying to upgrade from spring boot 2.3.8 to 2.4.3.

The projection in question includes two LocalDateTime fields, and is backed by a named query.

@NicklasWallgren
Copy link

NicklasWallgren commented Mar 14, 2021

@mp911de

The issue in my case particular case was that the column property was defined as ZonedLocalDateTime in the entity, and the Projection for the same field was defined as LocalDateTime. The error is a bit misleading (Projection type must be an interface), and I don't know if this can be considered as a bug, since the conversion from ZonedDateTime to LocalDateTime in projections worked in 2.3.

@schauder
Copy link
Contributor

@NicklasWallgren please open a new issue, since this on e is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

8 participants