I have an entity and I want it to have an id in a UUID table

When annotating a field like

@Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") @Column(name = "data_id") private UUID id; 

I get an error

  Ошибка: org.springframework.orm.jpa.JpaSystemException: Unknown integral data type for ids : java.util.UUID; nested exception is org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.util.UUID 

when calling a method to save this entity in the controller

 Data data1 = new Data(UUID.randomUUID(), другие параметры, , , ); dataRepository.save(data1); 

    1 answer 1

    In my opinion the problem is that hibernate cannot understand what kind of data it is. To solve the problem in a particular case, it is necessary to specify the type of the column explicitly, namely

     @Id @org.hibernate.annotations.Type(type = "pg-uuid") private UUID id; 

    in this case pg-uuid is a specific data type for postgres

    In general, it can be described as:

     import java.util.UUID; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** * JPA Convention to automatically convert UUID from Database (PostgreSQL) into Java and vice versa. */ @Converter(autoApply = true) public class UuidConverter implements AttributeConverter<UUID, UUID> { @Override public UUID convertToDatabaseColumn(UUID attribute) { return attribute; } @Override public UUID convertToEntityAttribute(UUID dbData) { return dbData; } }