2

I have defined a domain class which has "java.util.UUID" as its "Id" field.

@Entity
class Response{ 
 @Id
 @GeneratedValue(generator = "myUUIDGenerator")
 @GenericGenerator(name = "myUUIDGenerator", strategy = "uuid2")
 @Column(columnDefinition = "uuid")
 private UUID id;
 ...
}

I am using liquibase to generate the database .

<createTable tableName="response">
    <column name="id" type="uuid">
        <constraints primaryKey="true" nullable="false"/>
    </column>
</createTable>

The table generated in MySQL describes the generated id column as "char(36)".

The problem occurs while running test cases. It says the following and none of the test cases are executed.

Wrong column type in DBNAME_response for column id. Found: char, expected: uuid
Soumya
  • 1,833
  • 5
  • 34
  • 45

2 Answers2

1

In your Response class you are defining the id field as type UUID, but MySQL doesn't have a native UUID type, so it declares the column as char(36). What you might need to do is change it so that the field is a String, and then provide getter and setter methods that do the conversion String <-> UUID.

SteveDonie
  • 8,700
  • 3
  • 43
  • 43
0

As an update to the newer libraries, With JPA2.1 you shouldn't go SteveDonie's route -

Declare an Attribute Converter :

@Converter()
public class UUIDAttributeConverter implements AttributeConverter<UUID, String>, Serializable {

    @Override
    public String convertToDatabaseColumn(UUID locDate) {
        return (locDate == null ? null : locDate.toString());
    }

    @Override
    public UUID convertToEntityAttribute(String sqlDate) {
        return (sqlDate == null ? null : UUID.fromString(sqlDate));
    }

}

Mark the persistence unit as the converter applied (if not autoApply) in persistence.xml

<class>UUIDAttributeConverter</class>

Apply to fields

@Id
@Column(unique = true, nullable = false, length = 64)
@Convert(converter = UUIDAttributeConverter.class)
private UUID guid;

This allows you to specify the conversion should only take place in certain persistence units (such as your MySql) and not in others that do adhere. It also correctly maps the items through to db and keeps objects type safe.

Hope this helps!

Marc Magon
  • 713
  • 7
  • 11