Posts

Showing posts with the label Kotlin/spring

Kotlin and @Valid Spring Annotation

Image
To apply the validation before entering the domain level. I added the validation to my code in a similar way as in Java/Spring. However, the test kept failing since it doesn't validate the values. When I searched for how it works in Kotlin/Spring. I have realised I should tell Kotlin explicitly about the annotation target using @Field like below It is because properties in Kotlin are constructed in many various ways unlike Java such as, field, getter & setter method, and constructor parameter. That's why when the validation annotation like @Email or @Size are just used Kotlin compiler gets confused. Is it for getter or for field or for parameter? So we need to clarify by using @field: @get: @param:, etc. Link:  https://www.baeldung.com/kotlin/valid-spring-annotation

The problem of Data class in using JPA Entity in Kotlin

Image
 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.