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.

2
  • 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? Commented Mar 8, 2011 at 8:20
  • @JB Nizet, yes you're right, although I see I posted an incomplete question so I've updated it! Commented Mar 8, 2011 at 8:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.