Search code examples
javadatabasespring-boothibernatejpa

Save UUID as String in Database with JPA/Hibernate


normally I store my UUID´s to the database as String. In Spring Boot 2.x I used to decoreate the UUID with @Type(type = "org.hibernate.type.UUIDCharType"). Somehow in Spring Boot 3.x it doesnt work anymore. It tells me Cannot resolve method 'type'.

I know there were some changes to jakarta but i dont know if this is a problem to me.

My Entity:

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import java.util.Set;
import java.util.UUID;

@Data
@Entity
@Table(name = "werkstatt")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class WerkstattEntity {

    @Id
    @Column(columnDefinition = "VARCHAR(255)")
    @Type(type = "org.hibernate.type.UUIDCharType")
    private UUID id;
}

My gradle dep:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.springframework.security:spring-security-test'
    

    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api
    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'

    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl
    runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'

    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-jackson
    runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

    implementation 'org.mapstruct:mapstruct:1.5.5.Final'

    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'

}

Solution

  • As you'll find if you read in the migration guide for Hibernate 6, we no longer use stringly-typed names to identify types. Which is much better.

    But even better still, you no longer need a custom @Type annotation to persist UUIDs. So:

    @Id
    private UUID id;
    

    would in principle work fine.

    You can even use:

    @Id @GeneratedValue
    private UUID id;
    

    if you want the UUID to be generated.

    However, note that, by default, this maps to a column of type UUID if the database has such a type. If you really need to map to a VARCHAR columns, use:

    @Id @GeneratedValue
    @JdbcTypeCode(Types.VARCHAR)
    private UUID id;
    

    or:

    @Id @GeneratedValue
    @JdbcType(VarcharJdbcType.class)
    private UUID id;
    

    depending on taste.