1

I have a data model where I need a String column to be indexed by the backing database:

@Entity
public class A {
    // ... 

    @Column(length=2048)
    @Index(name="strFieldIndex")
    private String strField;
}

When adding the length attribute of @Column(length=2048) (for which hibernate doesn't generate varchar anymore) the following error message appears on MySQL:

ERROR org.hibernate.tool.hbm2ddl.SchemaExport - 
    BLOB/TEXT column 'strField' used in key specification without a key length

I've scanned the API docs for hibernate and I can't find an approach to setting the key length.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
  • 1
    AFAIK, this annotation is used when Hibernate generates the database schema for you, in order to create an index in the database. Do you let hibernate generate your schema? Is the index created? – JB Nizet Mar 08 '11 at 08:20
  • @JB Nizet, yes you're right, although I see I posted an incomplete question so I've updated it! – Johan Sjöberg Mar 08 '11 at 08:53

1 Answers1

2

I think you have to declare this index manually as required for MySQL, since Hibernate can't handle all DBMS-specific requirements. For example, using <database-object> syntax.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Is there any way to do this on a PRIMARY KEY? I guess I have to suppress Hibernate generating the primary key somehow, but that doesn't seem feasible. Is there any way to supply custom SQL for PRIMARY KEY generation? – rustyx Dec 27 '11 at 08:55