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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.