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

Ignore @Transient properties in constructors #2942

Open
sunny-chung opened this issue Sep 26, 2023 · 2 comments · May be fixed by #2985
Open

Ignore @Transient properties in constructors #2942

sunny-chung opened this issue Sep 26, 2023 · 2 comments · May be fixed by #2985
Assignees
Labels
type: enhancement A general enhancement

Comments

@sunny-chung
Copy link

sunny-chung commented Sep 26, 2023

This is a feature request. Currently querying an entity like this would fail:

@Table(name = "chat__channel")
data class ChatChannel(
    val name: String,
    @Transient val creationRequestId: String? = null
) : BaseEntity()

with exception:

java.lang.IllegalStateException: Required property creationRequestId not found for class com.example.entity.ChatChannel
	at org.springframework.data.mapping.PersistentEntity.getRequiredPersistentProperty(PersistentEntity.java:190) ~[spring-data-commons-3.1.2.jar!/:3.1.2]
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
	*__checkpoint ⇢ Handler com.example.api.ChannelApi#list(String, Continuation) [DispatcherHandler]
Original Stack Trace:
		at org.springframework.data.mapping.PersistentEntity.getRequiredPersistentProperty(PersistentEntity.java:190) ~[spring-data-commons-3.1.2.jar!/:3.1.2]
		at org.springframework.data.r2dbc.convert.MappingR2dbcConverter$RowParameterValueProvider.getParameterValue(MappingR2dbcConverter.java:715) ~[spring-data-r2dbc-3.1.2.jar!/:3.1.2]
		at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:49) ~[spring-data-commons-3.1.2.jar!/:3.1.2]
		at org.springframework.data.relational.core.conversion.BasicRelationalConverter$ConvertingParameterValueProvider.getParameterValue(BasicRelationalConverter.java:298) ~[spring-data-relational-3.1.2.jar!/:3.1.2]
		at org.springframework.data.mapping.model.KotlinClassGeneratingEntityInstantiator$DefaultingKotlinClassInstantiatorAdapter.extractInvocationArguments(KotlinClassGeneratingEntityInstantiator.java:222) ~[spring-data-commons-3.1.2.jar!/:3.1.2]
		at org.springframework.data.mapping.model.KotlinClassGeneratingEntityInstantiator$DefaultingKotlinClassInstantiatorAdapter.createInstance(KotlinClassGeneratingEntityInstantiator.java:196) ~[spring-data-commons-3.1.2.jar!/:3.1.2]
		at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:98) ~[spring-data-commons-3.1.2.jar!/:3.1.2]
		at org.springframework.data.relational.core.conversion.BasicRelationalConverter.createInstance(BasicRelationalConverter.java:136) ~[spring-data-relational-3.1.2.jar!/:3.1.2]
		at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.createInstance(MappingR2dbcConverter.java:328) ~[spring-data-r2dbc-3.1.2.jar!/:3.1.2]
		at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.read(MappingR2dbcConverter.java:120) ~[spring-data-r2dbc-3.1.2.jar!/:3.1.2]
		at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.read(MappingR2dbcConverter.java:115) ~[spring-data-r2dbc-3.1.2.jar!/:3.1.2]
		at org.springframework.data.r2dbc.convert.EntityRowMapper.apply(EntityRowMapper.java:42) ~[spring-data-r2dbc-3.1.2.jar!/:3.1.2]
		at org.springframework.data.r2dbc.convert.EntityRowMapper.apply(EntityRowMapper.java:29) ~[spring-data-r2dbc-3.1.2.jar!/:3.1.2]
		at io.asyncer.r2dbc.mysql.MySqlResult.lambda$map$1(MySqlResult.java:94) ~[r2dbc-mysql-1.0.2.jar!/:1.0.2]

...

Although there are many workarounds, it would be better if fields marked @Transient and have a default value in the constructor could be "ignored", so that we do not have to add another constructor to handle this use case.

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

mp911de commented Sep 26, 2023

Constructor arguments map to properties and @Transient is applied from the property declaration. Let me take your request to the team.

@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Sep 26, 2023
@christophstrobl christophstrobl self-assigned this Oct 2, 2023
@mp911de mp911de changed the title Add ignoring @Transient fields in constructors Ignore @Transient fields in constructors Nov 21, 2023
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged for: team-attention An issue we need to discuss as a team to make progress labels Nov 21, 2023
@mp911de mp911de assigned mp911de and unassigned christophstrobl Nov 21, 2023
@mp911de mp911de changed the title Ignore @Transient fields in constructors Ignore @Transient properties in constructors Nov 21, 2023
@mp911de
Copy link
Member

mp911de commented Nov 21, 2023

We consider adding support for @Transient properties in constructors. Record components could also be annotated with the @Transient annotation to allow record construction. While this can be useful, we recommend using the @Value annotation to use SpEL expressions to determine a useful value.

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
4 participants