我想通过xml映射在文本字段上添加一个索引。正如https://stackoverflow.com/a/860955/3256688中所建议的,我尝试使用辅助数据库对象,因为映射本身不支持索引长度。但对我来说,Hibernate似乎完全忽略了映射的这一部分。
这是我的密码。希望将索引url_idx添加到表MY_TABLE中。
hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
...
<mapping resource="mytable.hbm.xml"/>
...mytable.hbm.xml:
<hibernate-mapping>
<class name="com.company.MyTable" table="MY_TABLE">
...
<property name="url" column="url" type="text"/>
...
</class>
<database-object>
<create>CREATE INDEX url_idx ON MY_TABLE (`url`(30))</create>
<drop>DROP INDEX ON MY_TABLE `url_idx`</drop>
<dialect-scope name="org.hibernate.dialect.MySQL5InnoDBDialect"></dialect-scope>
</database-object>
</hibernate-mapping>在hibernate (调试级)的日志记录中,我看到每个表和索引的创建都是正确的,但我没有发现任何关于url_idx的信息。当然,数据库中没有创建索引。
知道我在这里错过了什么吗?
编辑:非常有趣:如果我使用工具org.hibernate.tool.hbm2ddl.SchemaExport进行模式导出,它就会正常工作。但是,如果我在代码中初始化Hibernate,情况仍然是一样的。
顺便说一句。我正在使用Hibernate 4.3.6
发布于 2014-12-08 13:51:51
我找到了解决方案:hibernate.hbm2ddl.auto必须是create或create-drop,否则代码将不会被执行。我觉得那是个虫子..。
发布于 2014-12-05 16:24:54
在这里,您创建了一个问题
<create>CREATE INDEX url_idx ON MY_TABLE (`url`(30))</create>你用
<drop>DROP INDEX ON MY_TABLE `url_idx`</drop>https://stackoverflow.com/questions/27320209
复制相似问题