How to use Hibernate Annotations to add an index on a Lob / Clob / tinyblob

后端 未结 3 1598
天命终不由人
天命终不由人 2021-01-06 04:33

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

相关标签:
3条回答
  • 2021-01-06 05:10

    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.

    0 讨论(0)
  • 2021-01-06 05:13

    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.

    0 讨论(0)
  • 2021-01-06 05:13

    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.

    0 讨论(0)
提交回复
热议问题