Hi guys, I’m currently trying to integrate Hibernate into my existing app to persist my game-state to a database.
Currently I’m stuck on defining a database SQL-Type for a custom class of mine.
In this case I have a class called “com.leo.commons.utils.UUID” and I want to persist every UUID to the database as a varchar(255).
Is there a way to do this? I already have a UUIDConverter that looks like this:
@Converter(autoApply = true)
public class UUIDConverter implements AttributeConverter<UUID, String> {
@Override
public String convertToDatabaseColumn(UUID uuid) {
return uuid.toString();
}
@Override
public UUID convertToEntityAttribute(String dbData) {
return new UUID(dbData);
}
}
My Entity looks like this:
@Entity
public class SphDatabaseEntity {
@Id
private UUID id;
....
}
When adding “@Column(columnDefinition = “varchar(255)”)” to the ID it seems to register the Type correctly, however I would need to add this line to every UUID in my code and I want to define it for every UUID in one place to avoid copy-pasting.
After some time debugging through the Hibernate-Implementation I found the place that seems to be responsible for determining the Hibernate-Type (which is then translated to the sqlType later): org.hibernate.type.TypeFactory#byClass
However the Type here can only be defined by extending the UUID with the “org.hibernate.type.Type”-class. This is not possible for me because “com.leo.commons.utils.UUID” does not know about hibernate and I would need to add the dependency to the project, which I want to avoid since this project has nothing to do with the persistence layer.
Currently when starting my app I get greeted with this dialog:
Could not determine type for: com.leo.commons.utils.UUID, at table: SphDatabaseEntity, for columns: [org.hibernate.mapping.Column(id)]
What other options do I have? Using “private String id” instead and creating the UUID in my buisness-code would be an option but I hope that there is another way that I just missed.
Would appreciate you advice
PS: If there is documentation to this that I missed please link me to it. Everything google is giving me are articles on how to do it with Spring Boot…