Skip to content

Projection results in mapping exception if entity has non nullable fields kotlin #2899

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
alirestum opened this issue May 13, 2024 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@alirestum
Copy link

If the enitity is a Kotlin data class with non nullable values the projection fails.

The following minimal project reproduces the error:

@Node("SomeEntity")
data class SomeEntity(
	@Id
	val id: String = UUID.randomUUID().toString(),
	val propertyOne: String,
	val propertyTwo: String
)

interface Projection {
	val propertyOne: String
}

@Repository
interface SomeEntityRepository : Neo4jRepository<SomeEntity, String> {
	fun findAllProjectedBy(): List<Projection>
}

@SpringBootApplication
class DemoApplication(
	private val someEntityRepository: SomeEntityRepository
) : CommandLineRunner {
	override fun run(vararg args: String?) {
		someEntityRepository.save(SomeEntity(propertyOne = "one", propertyTwo = "two"))
		val result = someEntityRepository.findAllProjectedBy()
	}

}

fun main(args: Array<String>) {
	runApplication<DemoApplication>(*args)
}

Error
org.springframework.data.mapping.MappingException: Error mapping Record<{someEntity: {propertyOne: "one", __elementId__: 11019, __nodeLabels__: ["SomeEntity"]}}>

Caused by: org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.example.demo.SomeEntity using constructor fun (kotlin.String, kotlin.String, kotlin.String): com.example.demo.SomeEntity with arguments null,one,null,1,null

Caused by: java.lang.NullPointerException: Parameter specified as non-null is null: method com.example.demo.SomeEntity.<init>, parameter propertyTwo

Spring Data Neo4j Version: 7.2.5
Neo4J Java Drivder Version: 5.19.0

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 13, 2024
@michael-simons michael-simons self-assigned this May 13, 2024
@michael-simons
Copy link
Collaborator

Thanks for the report. This is due to the fact that we rely in some scenarios on the real entity backing the projection. Fix is under way.

@michael-simons michael-simons added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 13, 2024
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

3 participants