简体   繁体   English

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

[英]Can I create both MyISAM and InnoDB tables in the same database using Hibenrate hbm2ddl

I need both MyISAM tables and InnoDB tables in my database, I am using hbm2ddl to create them. 我需要在我的数据库中使用MyISAM表和InnoDB表,我使用hbm2ddl来创建它们。 Can I create both MyISAM and InnoDB tables in the same database using Hibenrate hbm2ddl? 我可以使用Hibenrate hbm2ddl在同一个数据库中创建MyISAM和InnoDB表吗? It seems that selecting the dialect forces me to use one or the other. 似乎选择方言迫使我使用其中一种。

Well, as you wrote, Hibernate will generate InnoDB tables if you use the MySQL5InnoDBDialect : 好吧,正如您所写,如果您使用MySQL5InnoDBDialect ,Hibernate将生成InnoDB表:

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. 使用此方言将导致Hibernate在CREATE TABLE语句的末尾添加ENGINE=InnoDB 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. 要使用hbm2ddl并混合两个表引擎,我的建议是在事实之后更改特定的表。 To do so, you could use 5.7. 为此,您可以使用5.7。 Auxiliary database objects . 辅助数据库对象 From the documentation: 从文档:

5.7. 5.7。 Auxiliary database objects 辅助数据库对象

Auxiliary database objects allow for the CREATE and DROP of arbitrary database objects. 辅助数据库对象允许任意数据库对象的CREATE和DROP。 In conjunction with Hibernate's schema evolution tools, they have the ability to fully define a user schema within the Hibernate mapping files. 结合Hibernate的模式演化工具,他们能够在Hibernate映射文件中完全定义用户模式。 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.). 虽然专门用于创建和删除触发器或存储过程之类的东西,但任何可以通过java.sql.Statement.execute()方法运行的SQL命令都是有效的(例如,ALTER,INSERTS等)。 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: 第一种模式是在映射文件中显式列出CREATE和DROP命令:

 <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. 第二种模式是提供构造CREATE和DROP命令的自定义类。 This custom class must implement the org.hibernate.mapping.AuxiliaryDatabaseObject interface. 此自定义类必须实现org.hibernate.mapping.AuxiliaryDatabaseObject接口。

 <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 . 另一种选择是(ab)使用Hibernate import.sql功能来执行ALTER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM