UUID迷失了翻译?

时间:2016-12-05 22:46:04

标签: java mysql hibernate

我有一个UUID作为id的MySQL表。当我选择(在MySQL Workbench中)时,我得到:

269ecb9e-b7fe-11e6-a630-657416131b50
667d6caa-b800-11e6-a630-657416131b50
6f94b93c-b734-11e6-8618-ba2ba7c3db03
ab8478b8-b7fe-11e6-a630-657416131b50
ddb6b7d8-b80d-11e6-a630-657416131b50
fc031a90-b805-11e6-a630-657416131b50

然后我使用Hibernate在Java中查询,我得到:

32363965-6362-3965-2d62-3766652d3131
36363764-3663-6161-2d62-3830302d3131
36663934-6239-3363-2d62-3733342d3131
61623834-3738-6238-2d62-3766652d3131
64646236-6237-6438-2d62-3830642d3131
66633033-3161-3930-2d62-3830352d3131

正如你所看到的,我真的得不到相同的结果。

我的数据库架构使用char(36)并且为:

CREATE TABLE `table` (
  `id` char(36) NOT NULL COMMENT 'Unique identifier as a UUID.',
  `fragment` text COMMENT 'Generic field. '
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我的Hibernate POJO是:

package net.jgp.map.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.UUID;

@Entity
@Table(name = "table", catalog = "z_map")
public class ExternalMapping {

    private String id;
    private String fragment;

    @Id
    @Column(name = "id", unique = true, nullable = false)
    public UUID getId() {
        return java.util.UUID.fromString(this.id);
    }

    public void setId(UUID id) {
        this.id = id.toString();
    }

    @Column(name = "fragment")
    public String getFragment() {
        return this.fragment;
    }

    public void setFragment(String fragment) {
        this.fragment = fragment;
    }
}

其余的是经典的Hibernate代码(如果有帮助,很乐意分享)。

我的第一次尝试是没有toString()的东西,并在POJO代码中原生使用UUID,但结果是一样的。

0 个答案:

没有答案