Hibernate UUID as UUID type Hibernate UUID as UUID type postgresql postgresql

Hibernate UUID as UUID type


Update: with Hibernate 5 I was able to get more cross database compatible UUID's included (note: I am not the implementor, though I did give it a stab).

@Entitypublic class MyEntity {    @Id    @GeneratedValue    @Column( columnDefinition = "uuid", updatable = false )    public UUID getId() {        return id;    }}

Original

Dzone posted an article about a second version of the UUID generator last year

first create your own usertype (at least until hibernate implements similar)

public class UUIDType extends AbstractSingleColumnStandardBasicType<UUID> {public static final String NAME = "uuid-name";public static final UUIDType INSTANCE = new UUIDType();public UUIDType() {    super( UUIDSqlTypeDescriptor.INSTANCE, UUIDTypeDescriptor.INSTANCE );}public String getName() {    return NAME;}public static class UUIDSqlTypeDescriptor implements SqlTypeDescriptor {    public static final UUIDSqlTypeDescriptor INSTANCE = new UUIDSqlTypeDescriptor();    public int getSqlType() {        // ugh        return Types.VARCHAR;    }    @Override    public boolean canBeRemapped() {        return true;    }    public <X> ValueBinder<X> getBinder( final JavaTypeDescriptor<X> javaTypeDescriptor ) {        return new BasicBinder<X>( javaTypeDescriptor, this ) {            @Override            protected void doBind( PreparedStatement st, X value, int index, WrapperOptions options )                    throws SQLException {                st.setObject( index, javaTypeDescriptor.unwrap( value, UUID.class, options ) );            }        };    }    public <X> ValueExtractor<X> getExtractor( final JavaTypeDescriptor<X> javaTypeDescriptor ) {        return new BasicExtractor<X>( javaTypeDescriptor, this ) {            @Override            protected X doExtract( ResultSet rs, String name, WrapperOptions options ) throws SQLException {                return javaTypeDescriptor.wrap( rs.getObject( name ), options );            }            @Override            protected X doExtract( CallableStatement statement, int index, WrapperOptions options )                    throws SQLException {                return javaTypeDescriptor.wrap( statement.getObject( index ), options );            }            @Override            protected X doExtract( CallableStatement statement, String name, WrapperOptions options )                    throws SQLException {                return javaTypeDescriptor.wrap( statement.getObject( name ), options );            }        };    }}}

Then annotate your models package-info.java

 @TypeDef(    name = UUIDType.NAME,    defaultForType = UUID.class,    typeClass = UUIDType.class)package com.xenoterracide.rpf.model;import org.hibernate.annotations.TypeDef;import org.hibernate.type.UUIDType;import java.util.UUID;

finally in your entity

@Id@Override@GeneratedValue(generator = "uuid2")@GenericGenerator(name = "uuid2", strategy = "uuid2")@Column(name = "id", columnDefinition = "uuid")public UUID getId() {    return id;}