2

我已经定义了一个具有“java.util.UUID”作为其“Id”字段的域类。

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

我正在使用 liquibase 生成数据库。

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

MySQL 中生成的表将生成的 id 列描述为“char(36)”。

运行测试用例时出现问题。它说以下内容并且没有执行任何测试用例。

Wrong column type in DBNAME_response for column id. Found: char, expected: uuid
4

2 回答 2

1

在您的Response类中,您将id字段定义为 type UUID,但 MySQL 没有本机UUID类型,因此它将列声明为char(36). 您可能需要做的是更改它以使该字段为 a String,然后提供执行转换的 getter 和 setter 方法String <-> UUID

于 2017-03-28T15:38:51.160 回答
0

作为对较新库的更新,使用 JPA2.1,您不应该走 SteveDonie 的路线 -

声明一个属性转换器:

@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));
    }

}

在 persistence.xml 中将持久性单元标记为应用的转换器(如果不是自动应用)

<class>UUIDAttributeConverter</class>

应用于字段

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

这允许您指定转换应该只发生在某些持久性单元(例如您的 MySql)中,而不是在其他坚持的情况下。它还正确地将项目映射到 db 并保持对象类型安全。

希望这可以帮助!

于 2018-01-02T09:12:50.743 回答