gpt4 book ai didi

mysql - 我可以使用 Hibenrate hbm2ddl 在同一个数据库中创建 MyISAM 和 InnoDB 表吗

转载 作者:可可西里 更新时间:2023-11-01 08:38:08 25 4
gpt4 key购买 nike

我的数据库中同时需要 MyISAM 表和 InnoDB 表,我正在使用 hbm2ddl 来创建它们。我可以使用 Hibenrate hbm2ddl 在同一个数据库中创建 MyISAM 和 InnoDB 表吗?似乎选择方言迫使我使用其中一种。

最佳答案

好吧,正如您所写,如果您使用 MySQL5InnoDBDialect,Hibernate 将生成 InnoDB 表:

public class MySQL5InnoDBDialect extends MySQL5Dialect {

public boolean supportsCascadeDelete() {
return true;
}

public String getTableTypeString() {
return " ENGINE=InnoDB";
}

public boolean hasSelfReferentialForeignKeyBug() {
return true;
}

}

使用此方言将导致 Hibernate 在 CREATE TABLE 语句的末尾添加 ENGINE=InnoDB。但这是全局设置,您不能在实体级别调整此行为。

要使用 hbm2ddl 并混合两个表引擎,我的建议是事后更改特定表。为此,您可以使用 5.7. Auxiliary database objects .来自文档:

5.7. Auxiliary database objects

Auxiliary database objects allow for the CREATE and DROP of arbitrary database objects. In conjunction with Hibernate's schema evolution tools, they have the ability to fully define a user schema within the Hibernate mapping files. Although designed specifically for creating and dropping things like triggers or stored procedures, any SQL command that can be run via a java.sql.Statement.execute() method is valid (for example, ALTERs, INSERTS, etc.). There are essentially two modes for defining auxiliary database objects:

The first mode is to explicitly list the CREATE and DROP commands in the mapping file:

<hibernate-mapping>
...
<database-object>
<create>CREATE TRIGGER my_trigger ...</create>
<drop>DROP TRIGGER my_trigger</drop>
</database-object>
</hibernate-mapping>

The second mode is to supply a custom class that constructs the CREATE and DROP commands. This custom class must implement the org.hibernate.mapping.AuxiliaryDatabaseObject interface.

<hibernate-mapping>
...
<database-object>
<definition class="MyTriggerDefinition"/>
</database-object>
</hibernate-mapping>

Additionally, these database objects can be optionally scoped so that they only apply when certain dialects are used.

<hibernate-mapping>
...
<database-object>
<definition class="MyTriggerDefinition"/>
<dialect-scope name="org.hibernate.dialect.Oracle9iDialect"/>
<dialect-scope name="org.hibernate.dialect.Oracle10gDialect"/>
</database-object>
</hibernate-mapping>

另一种选择是(ab)使用 Hibernate import.sql feature执行 ALTER

关于mysql - 我可以使用 Hibenrate hbm2ddl 在同一个数据库中创建 MyISAM 和 InnoDB 表吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3381307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com