我使用的 hibernate 版本:4.3.8.Final
在 web.xml 中我有: `
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.validator.apply_to_ddl" value="true" />
<property name="hibernate.connection.CharSet" value="utf8" />
<property name="hibernate.connection.characterEncoding" value="utf8" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="300"/>`
当我运行应用程序时,hibernate 会生成以下位置的所有表:latin1_swedish_ci
我在谷歌上阅读了很多页面,但没有任何帮助。
如何使用 hibernate 生成 utf8 表? 有可能吗?
感谢您的帮助。
最佳答案
如果你检查 org.hibernate.dialect.MySQL5InnoDBDialect 实现,你会发现:
package org.hibernate.dialect;
/**
* @author Gavin King, Scott Marlow
*/
public class MySQL5InnoDBDialect extends MySQL5Dialect {
public boolean supportsCascadeDelete() {
return true;
}
public String getTableTypeString() {
return " ENGINE=InnoDB";
}
public boolean hasSelfReferentialForeignKeyBug() {
return true;
}
}
所以你可以在getTableTypeString中看到返回值,你可以做的是扩展MySQLDialect类并重写getTableTypeString方法来返回
"ENGINE=InnoDB DEFAULT CHARSET=utf8";
否则(您可以尝试懒惰的解决方案)尝试在数据库连接网址末尾添加 UseUnicode=true&characterEncoding=utf8
关于java - 如何使用 hibernate 生成 UTF8 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41903542/