However, if you use Hibernate to query through Kotlin JDSL without extra configuration, you will get an error.
org.hibernate.type.descriptor.java.CoercionException: Cannot coerce value 'UserId(value=1)' [com.example.entity.UserId] to Long
...
To solve this, Kotlin JDSL requires unboxing of the value class passed as a parameter. Unboxing can be done in one of the following ways
Custom JpqlSerializer for JpqlValue
To resolve the error, you need to pass the unboxed value to the EntityManager instead of passing the arguments to the value class itself. Kotlin JDSL is supposed to extract the arguments from the JpqlValueSerializer class. So, we need to register a custom Seriailzer instead of the built-in class.
Now we need to add this class to our RenderContext. You can refer to the following documentation for how to add it. If you are using Spring Boot, you can register your custom Seriziler as a bean with the following code.
If you use value class in DTO Projection, it is not supported if the property is nullable. Therefore, rather than using value class directly in DTO Projection, it is recommended to use the default type and convert it after querying.