Java.util.UUID在Mysql DB

时间:2017-05-18 22:01:18

标签: java mysql hibernate nativequery hibernate-native-query

public class Test{
    @Id
    @JsonIgnore
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name="TEST_ID",columnDefinition = "BINARY(16)")
    @Type(type="uuid-binary")
    //@Column(name="TEST_ID",columnDefinition = "BINARY(32)")
    //@Type(type="uuid-char")
    private UUID testId;

    @Id
    @JsonIgnore
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name="USER_ID",columnDefinition = "BINARY(16)")
    //@Column(name="USER_ID",columnDefinition = "BINARY(32)")
    //@Type(type="uuid-binary") //@Type(type="uuid-char")
    private UUID userId;

    public void setTestId(UUID testId) {
        this.testId = testId;
    }

    public UUID getTestId() {
        return testId;
    }    

}

将数据保存在数据库中。 UUID在DB中保存为BLOB。 从DB获取ID时出错

test.setTestId( UUID.nameUUIDFromBytes((byte[]) row[0]));

Mysql表:

DROP TABLE IF EXISTS `DBName`.`testtable` ;
CREATE TABLE IF NOT EXISTS `DBName`.`testtable` (
`test_id` binary(16) NOT NULL,
`user_id` binary(16) NOT NULL,
`company_id` VARCHAR(45) NOT NULL,
PRIMARY KEY (`test_id`),
UNIQUE KEY `company_test_id` (`company_id`, `test_id`))
ENGINE = InnoDB;
尝试了解决方案:

//@Column(name="TEST_ID",columnDefinition = "BINARY(32)")
//@Type(type="uuid-binary") //@Type(type="uuid-char")

没有任何效果。

0 个答案:

没有答案