Posts

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.

Entity vs Value

 Entity - Entity has a identifier. The identifier is unique for each entity. That's why each entity has a different identifier from each other.  The timing of generating an entity's identifier varies depending on the characteristics of the domain and the technology used. Generates based on specific rules UUID or Nano ID -> Unique Identifier Generator Manually input value Serial Number Value -  A value type is used to represent a conceptually complete whole.

Asynchronous Event Handling

Image
When implementing the notification feature for creating a new room, which sends notifications to users with matching tags and zones, I realized it should not interfere with the core room creation logic. If the notification process throws errors, it could cause the room creation to roll back. To prevent this, I decided to use @Async to handle notifications asynchronously. <Service> <To Handle Events> However, there was one problem left. The basic @EnableAsync uses SimpleAsyncTaskExecutor so, it creates a new Thread for each task. To prevent it, I override getAsuncExecutor() by implementing AsyncConfigurer.

ModelMapper: A Cleaner Approach to Data Mapping

Image
The Problem: Repetitive Code and Human Error In my project, I had three key classes: AccountService, Profile, and Notification. These classes were responsible for handling data, but their code was way too repetitive . The names were so similar that I occasionally forgot to include all the necessary information or made silly mistakes. 😅 This not only slowed me down but also made debugging a nightmare. <Before> * AccountService.Class *Profile.Class *Notification.Class The Solution: Enter ModelMapper To tackle this, I decided to integrate ModelMapper , a fantastic library that automates object mapping and reduces boilerplate code. ModelMapper makes it super easy to map properties between objects, saving you from writing endless getters and setters. Here’s how I implemented it, step by step: Step 1: Add ModelMapper Dependency Step 2: Configure ModelMapper as a Bean To avoid creating multiple instances of ModelMapper (which could lead to performance issues), I decided to register i...

05.05.2025 - Uutiset

https://yle.fi/a/74-20159842 Jauhelihapula pula - shortage - Minced meat shortage. Aluksi uutinen kotimaasta. - First, news from home country. Kaupoissa on pulaa naudan jauhelihasta. Tilanne on jatkunut jo muutaman kuukauden. - There is a shortage of minced beef in stores. The situation has been going on for a few months now. Syy on kysynnän kasvu ja tuotannon väheneminen. - The reason is the increase in demand and the decrease in production. Lehmien määrä Suomessa pienenee. - The number of cows in Finland is decreasing. Yhdestä lehmästä saadaan maitoa enemmän kuin ennen. Siksi lehmiä ei tarvita maitotilalla yhtä paljon kuin ennen. - More mile is produced from one cow than before. That's why there is not as much need for cows on a dairy farm as before. Myös lihakarjan kasvatuksen kannattavuus on heikentynyt. heikentyä - to weaken, kannattavuus - profitability. - The profitability of beef cattle farming has also declined. Kaupoissa on pulaa naudan jauhelihasta, koska lehmien määrä v...

Solve - Spring Circular Reference

Image
  What is a Spring Circular Reference? A circular reference in Spring occurs when two or more beans depend on each other, causing Spring to be unable to determine which bean should be created first. This issue arises in the context of Dependency Injection (DI) , which can be implemented in three ways: Setter Injection , Field Injection , and Constructor Injection . Circular References with Different Injection Types Setter and Field Injection : Circular reference issues do not occur during application loading with Setter or Field Injection. This is because dependencies are injected only when the bean is actually used (e.g., when a method is called), not during the bean creation phase. As a result, circular reference problems manifest at runtime when the relevant method is invoked. Constructor Injection : Circular references do occur during application loading with Constructor Injection. This happens because Spring must inject the referenced beans at the time of bean cr...