David
David

Reputation: 20063

Hibernate using Uuid generation

There seems to be quite a few questions on this, but none resolve my issue.

I'm trying to use hibernate annotations to generate a UUID.

My annotations are below...

@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "uuid", unique = true)
public UUID getUuid() {
    return uuid;
}

I'm using MySQL 5.2 with Hibernate 3.5.6 in my pom.xml as shown below...

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

Upon starting my application, I get the following error...

ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table players (uuid tinyblob not null unique, espnid integer, espnUrl varchar(255), firstname varchar(255), lastname varchar(255), primary key (uuid))
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - BLOB/TEXT column 'uuid' used in key specification without a key length

What's the correct annotation? Am I using an incorrect hibernate version? Am I using something incorrectly with MySQL?

My dialect details are below...

<beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">create</beans:prop>
            </beans:props>
        </beans:property>

The error implies the length isn't specified, but if I enter this into @Column nothing changes.

I'm only using an older version of hibernate due to the hibernate-annotations only going up till then, if this is now a dead repo I'll move to a later version.

Upvotes: 1

Views: 5696

Answers (1)

dcernahoschi
dcernahoschi

Reputation: 15240

I think the problem is the type returned by the getUuid() method. It needs to be a String according to hibernate docs.

uuid:

uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.

Upvotes: 3

Related Questions