Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store uuid in binary form using hibernate JPA 2

I have a question about string uuid in database in binary form through hibernate persistence (JPA2). I'm using now this code:

private UUID id;

@Id
@Type(type="uuid-char")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(length = 32, unique = true, nullable = false)
public final UUID getId() {
    return id;
}

This work fine, but I must store it in binary form. Don't ask me why, but I must.

like image 248
pierre tautou Avatar asked Jun 29 '11 14:06

pierre tautou


People also ask

How to use UUID in Hibernate?

if you want a Human-Readable varchar(36) field in the database table, but also a Serializable UUID data type in your Java Class, you can use @Type(type = "uuid-char") at the same time you declare your field member with java.

How to generate UUID in JPA?

Generating UUIDs using JPA 3.1 Since JPA 3.1, you can annotate a primary key attribute with @GeneratedValue and set the strategy to GenerationType. UUID. Based on the specification, your persistence provider shall generate a UUID value based on IETF RFC 4122. Let's try this mapping and persist a new Book entity object.


1 Answers

The type for binary UUID is uuid-binary. You must have Hibernate 3.6 for this to work.

For many more details and pitfalls, see the answers to this question.

like image 90
Adrian Cox Avatar answered Oct 03 '22 14:10

Adrian Cox