Skip to content

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

Closed
@igorwojda

Description

@igorwojda

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions