The problem of Data class in using JPA Entity in Kotlin

 When I wrote this test, I expected that "DataIntegrityViolationException" is thrown.





Since I put @NaturalId in email, it seemed that there was no problem.















When I debugged to find out what the problem was, the member objects were successfully saved even though they had same email value.









I found out it was from me poorly understanding how it works when using data class with JPA entity.

This is the link: https://github.com/spring-guides/tut-spring-boot-kotlin#persistence-with-jpa

Since data class creates automatically equals/hashCode, data class's equal compares all the filed. Therefore, id value was different so it recognized they were different entities even though they were same entity.

Comments

Popular posts from this blog

@ModelAttribute vs @RequestBody in Validation

Side Project(a self-imposed 3-day "Hackathon" challenge)

Google: The King is Back