简体   繁体   中英

Kotlin package level TypeDefs for Hibernate

I have a custom type for hibernate ( TipoSiNo ), this is the equivalent of the YesNoType but for spanish (S/N).

When using java I declared this type as the default for boolean fields in a package-info.java file:

@TypeDefs(@TypeDef(name = "si_no", typeClass = TipoSiNo.class, defaultForType = Boolean.class))
package com.xyz;

import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import com...TipoSiNo;

This worked fine for java entity classes but when I convert them to kotlin I got an error:

Data conversion error converting "'S'...

So I supossed I had to convert the package-info file to kotlin:

@file:TypeDefs(TypeDef(name = "si_no", typeClass = TipoSiNo::class, defaultForType = Boolean::class))

package com.xyz

import org.hibernate.annotations.TypeDef
import org.hibernate.annotations.TypeDefs

import com...TipoSiNo

But even then it does not seem to work.

There is currently no equivalent of package-info.java for Kotlin. As a workaround, you can simply include a package-info.java in Kotlin and it will be processed.

If you are using Spring Boot, you may run into this additional issue: How to configure package level @TypeDefs with Spring Hibernate :

It seems spring have some issue to scan TypeDef when they are at package-info.java class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM