Skip to content

Allow to use Kotlin value classes directly as JpaRepository Id type #2840

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
igorwojda opened this issue Mar 5, 2023 · 3 comments
Closed
Labels
for: team-attention An issue we need to discuss as a team to make progress in: kotlin Kotlin support type: enhancement A general enhancement

Comments

@igorwojda
Copy link

Consider this entity

@Entity
@Table(name = "project")
class ProjectJpa(

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    val id: ProjectId,

    @Column(name = "name", nullable = false)
    val name: String,
)

The id is defined as Kotlin value class that is just a wrapper for the UUID:

value class ProjectId(val id: UUID)

Define repo with ProjectId as id

@Repository
interface ProjectJpaRepository : JpaRepository<ProjectJpa, ProjectId>

With the above code project can be saved to database:

projectJpaRepository.save(project.toJpa())

Problem
Retrieving project using findById...

projectJpaRepository.findById(ProjectId(UUID.fromString("1ec87062-89dd-4f75-a7a2-be7631332bbb")))

... results in 'org.springframework.dao.InvalidDataAccessApiUsageException' exception.:

Provided id of the wrong type for class com.mango.persistence.entity.ProjectJpa. Expected: 
class java.util.UUID, got class com.mango.business.model.value.ProjectId
@mp911de
Copy link
Member

mp911de commented Apr 11, 2023

Do you have minimal sample that reproduces the problem for us?

Generally, we do not yet support inline classes because Kotlin compiles bytecode that isn't aligning well with bean property names and constructor resolution requires as well some workarounds.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Apr 11, 2023
@igorwojda
Copy link
Author

Here it is
sample-2840.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 Apr 11, 2023
@mp911de
Copy link
Member

mp911de commented Apr 11, 2023

Kotlin compiles ProjectJpa to:

class ProjectJpa {

UUID id; // Kotlin: ProjectId
}

while the repository declaration remains at ProjectJpaRepository extends JpaRepository<ProjectJpa, ProjectId>. This difference between the declarations causes the identifier mismatch. You could switch the repository declaration to JpaRepository<ProjectJpa, UUID>.

There's not much we can do here. Kotlin aims to provide a lot of value at the code frontend at the price of compatibility between components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-attention An issue we need to discuss as a team to make progress in: kotlin Kotlin support type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants