I have a bean which I map to the database using Hibernate. I\'m using Hibernate Annotations to indicate the mapping I want, and to create the indices. The thoroughly simplif
You could do this using Hibernate's auxiliary objects support, but it cannot be done using annotations :-(.
In your example, it would look something like this (lots of stuff omitted for brevity):
<class name="Person" table="persons">
<!-- whatever -->
<database-object>
<create>create index sysuuid on persons ( system, `uuid`(8) )</create>
<drop>drop index sysuuid</drop>
<dialect-scope name="org.hibernate.dialect.MySQL5InnoDBDialect" />
</database-object>
</class>
I apologize for the lack of an annotation-based answer :-(. Hopefully this helps.
NOTE: If you do take this approach, be aware that the dialect scope has to match exactly. For example, if your Hibernate configuration says to use MySQL5InnoDBDialect
, then you must have this dialect in the <dialect-scope>
element as well. Using MySQLDialect
will not work even though it is the super-class of the InnoDB dialect.
What you really want to do is to define your UUID property as a String rather than a byte array. That way Hibernate will map it to a character column in the database rather than a LOB, and I think it'll create the index you want.
Here is my research results with MySQL. If you have a field which length is big:
@Column(length = 2000)
public String getMessageId() {
return messageId;
}
then its type would be BLOB and indexing would not work:
HHH000389: Unsuccessful: create index messageid_idx on MessageDetails (messageId)
BLOB/TEXT column 'messageId' used in key specification without a key length
If you use annotations, the only way to add custom sql is the following:
cfg.addAuxiliaryDatabaseObject(new SimpleAuxiliaryDatabaseObject(
"CREATE INDEX messageId_IDX ON MessageDetails(MessageID(128))",
"DROP INDEX messageId_IDX ON MessageDetails"));
where cfg is object that is used for registering annotations:
cfg.addAnnotatedClass(MessageDetails.class);
this works for me. Thanx for your attention.