gpt4 book ai didi

java - 让 JPA 生成具有指定行格式的表

转载 作者:行者123 更新时间:2023-11-29 02:22:45 25 4
gpt4 key购买 nike

我在 JPA hibernate 中使用 play 2 框架。我的目标是让 JPA 使用特定的 ROW_FORMAT 在新数据库上生成表:

这是 JPA 在启动时执行的内容:

create table Admin (
..
) ENGINE=InnoDB

这就是我想要的:

create table Admin (
..
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC

persistance.xml 如下所示:

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>

我尝试寻找在 Java 代码中使用注释的方法,或者在 XML conf 中寻找特定属性。我运气不好。

最佳答案

您可以通过子类化 MySQL5InnoDBDialect 并覆盖 getTableTypeString() 方法来实现,如下所示。

public class MySQL5CustomInnoDBDialect extends MySQL5InnoDBDialect{

/**
* Call to super returns " type=innodb". 'Type' is a
* deprecated MySQL synonym for 'engine'
* so you may want to ignore the super call and use:
* " engine=innodb ROW_FORMAT=DYNAMIC"
*/
public String getTableTypeString() {
return super.getTableTypeString() + " ROW_FORMAT=DYNAMIC";
}
}

显然将其设置为您的方言:

<property name="hibernate.dialect" value="org.foo.MySQL5CustomInnoDBDialect"/>

关于java - 让 JPA 生成具有指定行格式的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632984/

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