Hibernate UUID as UUID type

淺唱寂寞╮ 提交于 2019-12-05 07:11:38

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).

@Entity
public 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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!