I have a project I am working on that uses UUIDs. Unfortunately, when I try to query my DB to check if a specific UUID exists it always returns false even if I can see it in it. To check that my program works fine I switched UUIDs to normal Long IDs and all works fine. Any help would be appreciated.
This is how I generate UUIDs in my Entity:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(insertable = false, updatable = false, nullable = false)
private UUID uuid;
This is my logic check in my service class:
boolean exists = technologyRepository.existsByUuid(t.getUuid());
if (!exists) {
throw new IllegalStateException("Technology with id " t.getUuid() " does not exist." );
}
exists
always returns false when using UUIDS
java.lang.IllegalStateException: Technology with id 61b78428-8bbf-4d84-997f-62e0484c21cc does not exist.
this is the UUID stored in my DB:
61b784288bbf4d84997f62e0484c21cc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The following is the error message I get by running the query on my H2 server directly:
Values of types "BINARY(255)" and "CHARACTER VARYING(32)" are not comparable; SQL statement: SELECT * FROM TECHNOLOGY WHERE UUID = '61b784288bbf4d84997f62e0484c21cc' [90110-214] 90110/90110
CodePudding user response:
Adding the parameter columnDefinition = 'BINARY(16)
to the @Column
annotation solved the issue
CodePudding user response:
in my case I used "String" for the field with UUID in the Java class mapping the table. The column in the table on the database is a VARCHAR. It has worked ok for me.
@Entity
@Access(AccessType.FIELD)
@Table(name = "TABLE_WITH_UUID")
@NoArgsConstructor
@AllArgsConstructor
public class TableWithUuid implements Serializable {
private static final long serialVersionUID = 1L;
[... other fields ...]
@Column
@Getter
@Setter
private String uuid;
[... other fields ....]
The code to create the UUID is the following:
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString();
I hope it can help.