Skip to content
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

Investigate support for JDK 14 record types in object mapping [DATACMNS-1636] #2062

Open
spring-projects-issues opened this issue Dec 10, 2019 · 5 comments
Labels
in: mapping type: documentation

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Dec 10, 2019

Oliver Drotbohm opened DATACMNS-1636 and commented


No further details from DATACMNS-1636

@spring-projects-issues spring-projects-issues added type: enhancement in: mapping labels Dec 30, 2020
@sothawo
Copy link

@sothawo sothawo commented May 1, 2021

Hello,

what's the state of this? I just tried to use a recordas entity type in Spring Data Elasticsearch and got

2021-05-01 16:11:20.839 ERROR 77582 --- [nio-9090-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.sothawo.springdataelastictest.presidents.President using constructor NO_CONSTRUCTOR with arguments ] with root cause

java.lang.NoSuchMethodException: com.sothawo.springdataelastictest.presidents.President.<init>()
	at java.base/java.lang.Class.getConstructor0(Class.java:3517) ~[na:na]
	at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2691) ~[na:na]
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) ~[spring-beans-5.3.6.jar:5.3.6]
	at org.springframework.data.mapping.model.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:62) ~[spring-data-commons-2.6.0-20210427.084756-6.jar:2.6.0-SNAPSHOT]
	at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:89) ~[spring-data-commons-2.6.0-20210427.084756-6.jar:2.6.0-SNAPSHOT]
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter$Reader.readEntity(MappingElasticsearchConverter.java:321) ~[spring-data-elasticsearch-4.3.0-SNAPSHOT.jar:4.3.0-SNAPSHOT]

using this class (Term is another record class):

@Document(indexName = "presidents")
public record President(
    @Id
    String id,

    @Field(type = FieldType.Text)
    String name,

    @Field(type = FieldType.Integer_Range)
    Term term) {
}

@mp911de
Copy link
Member

@mp911de mp911de commented May 1, 2021

Records are supported since quite a while and persistence constructor detection works as with other classes. For some reason, the entity instantiator tries to use a no-arg constructor

@sothawo
Copy link

@sothawo sothawo commented May 1, 2021

ok, I have an additional ctor in there which I omitted above, when annotating the compact ctor it works:

@Document(indexName = "presidents")
public record President(
    @Id
    String id,

    @Field(type = FieldType.Text)
    String name,

    @Field(type = FieldType.Integer_Range)
    Term term) {

    @PersistenceConstructor
    public President{}

    public President(String name, Term term) {
        this(UUID.randomUUID().toString(), name, term);
    }
}

@indiepopart
Copy link

@indiepopart indiepopart commented Sep 29, 2021

Records are supported since quite a while and persistence constructor detection works as with other classes. For some reason, the entity instantiator tries to use a no-arg constructor

I can't find a note in spring-data-commons reference about records. Would you please point me to documentation where it is mentioned?

@mp911de
Copy link
Member

@mp911de mp911de commented Sep 30, 2021

Records are classes with a specific naming scheme after all and those work out of the box. However, we do recommend using records for DTO's as they are not a good fit as entities. We should use this ticket to document the recommendation.

@mp911de mp911de added type: documentation and removed type: enhancement labels Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping type: documentation
Projects
None yet
Development

No branches or pull requests

4 participants