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

发布于 2024-09-12 11:53:18 字数 123 浏览 0 评论 0原文

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

I need both MyISAM tables and InnoDB tables in my database, I am using hbm2ddl to create them. Can I create both MyISAM and InnoDB tables in the same database using Hibenrate hbm2ddl?
It seems that selecting the dialect forces me to use one or the other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-09-19 11:53:18

好吧,正如您所写,如果您使用 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 在 末尾添加 ENGINE=InnoDB >CREATE TABLE 语句。但这是全局设置,您无法在实体级别调整此行为。

要使用 hbm2ddl 并混合使用两个表引擎,我的建议是在事实发生后更改特定表。为此,您可以使用 5.7。辅助数据库对象。从文档中:

5.7。辅助数据库对象

辅助数据库对象允许
任意的 CREATE 和 DROP
数据库对象。结合
Hibernate 的模式演化工具,
他们有能力完全定义
Hibernate 中的用户模式
映射文件。虽然设计的
专门用于创建和删除
诸如触发器或存储之类的东西
过程,任何可以的 SQL 命令
通过运行
java.sql.Statement.execute() 方法是
有效(例如,ALTER、INSERTS、
ETC。)。本质上有两种模式
用于定义辅助数据库
对象:

第一种模式是显式列出
CREATE 和 DROP 命令在
映射文件:

;
    ...
    <数据库对象>
        <创建>创建触发器 my_trigger ...
        DROP TRIGGER my_trigger
    

第二种模式是提供自定义
构造 CREATE 和
删除命令。这个自定义类必须
实施
org.hibernate.mapping.AuxiliaryDatabaseObject
界面。

;
    ...
    <数据库对象>
        <定义类=“MyTriggerDefinition”/>
    

此外,这些数据库对象
可以选择范围,以便它们
仅适用于某些方言
使用过。

;
    ...
    <数据库对象>
        <定义类=“MyTriggerDefinition”/>
        >
        >
    

另一种选择是 (ab) 使用 Hibernate import.sql 功能 执行 更改。

Well, as you wrote, Hibernate will generate InnoDB tables if you use the MySQL5InnoDBDialect:

public class MySQL5InnoDBDialect extends MySQL5Dialect {

    public boolean supportsCascadeDelete() {
        return true;
    }

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

    public boolean hasSelfReferentialForeignKeyBug() {
        return true;
    }

}

Using this dialect will cause Hibernate to add the ENGINE=InnoDB at the end of the CREATE TABLE statements. But this is global setting, you can't tweak this behavior at the entity level.

To use hbm2ddl and mix both tables engines, my suggestion would be to ALTER specific tables after the facts. To do so, you could use 5.7. Auxiliary database objects. From the documentation:

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>

Another option would be to (ab)use the Hibernate import.sql feature to perform the ALTER.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文