Problems mapping UUID in JPA/hibernate

前端 未结 6 1146
既然无缘
既然无缘 2020-12-05 10:20

According to the documentation, hibernate 3.6 should have support for the java.util.UUID type. But when I map it like:

@Id protected UUID uuid;
6条回答
  •  天命终不由人
    2020-12-05 10:44

    Using Hibernate 4 and MySQL 5.5 with an InnoDB table, I was able to store a UUID column as BINARY(16) as-is (no configuration or custom type required). I am not using this as the entity ID and am creating the value manually using UUID.randomUUID().

    @Entity
    @Table(name = "post")
    public class PostModel implements Serializable
    {
        ...
        @Column(name = "uuid", nullable = false, updatable = false)
        private UUID uuid;
        ...
    }
    
    > desc post;
    +----------------+---------------+------+-----+---------------------+
    | Field          | Type          | Null | Key | Default             |
    +----------------+---------------+------+-----+---------------------+
    | ...            |               |      |     |                     |
    | uuid           | binary(16)    | YES  | UNI | NULL                |
    | ...            |               |      |     |                     |
    +----------------+---------------+------+-----+---------------------+
    

提交回复
热议问题