Reputation: 21
We are migrating from Hibernate 5 to 6, and are having issues with the conversion of our id.
@MappedSuperclass
public abstract class Entity {
@Id
@Column(name = "ID")
@Convert(converter = UUIDConverter.class)
private UUID id = UUID.randomUUID();
}
The converter is set up like this:
@Converter
public class UUIDConverter implements AttributeConverter<UUID, String> {
@Override
public String convertToDatabaseColumn(UUID attribute) {
return attribute.toString();
}
@Override
public UUID convertToEntityAttribute(String dbData) {
return UUID.fromString(dbData);
}
}
We are using Sql Server, and the id is a uniqueidentifier in the database. If we save an object with id 8f935c03-0971-445e-9526-0ecbc743b470
, this will be saved in the database as 035C938F-7109-5E44-9526-0ECBC743B470
.
Any suggestion on how to solve this? What is a best-practice way to handle id`s? Some documentation say that we should not combine @Id with @Convert, but we have not found out what the alternative is.
We have tried converting to uppercase in the converter and we have tried using in IdClass.
Upvotes: 2
Views: 591
Reputation: 4303
JPA now comes with support for UUID
built-in, so it should be as simple as:
@MappedSuperclass
public abstract class Entity {
@Id
@GeneratedValue
private UUID id;
}
Or, if you want some more control, check out the @UuidGenerator
annotation in org.hibernate.annotations
.
https://docs.jboss.org/hibernate/orm/6.2/javadocs/org/hibernate/annotations/UuidGenerator.html
Upvotes: 1