java - 如何使用 hibernate 生成 UTF8 表

标签 java spring hibernate

我使用的 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/

相关文章:

hibernate - Grails & HibernateException : connnection proxy not usable after transaction completion

java - 使用 IN 子句和子选择查询多列的 Hibernate Criteria Query

java - 环境变量和@Value 不能在 Spring Boot 上协同工作

java - JLabel 没有出现在 JFrame 中

mysql - spring data JPA - mysql - findById() 为空,除非之前调用了 findAll()

java - DAO 在 Spring JPA 中是一个好的实践吗?

java - 为基于 Hibernate 的应用程序实现数据历史记录/版本控制解决方案(有一个转折点)

java - 保存具有一对多关系的实体

java - 如何使用java执行t-sql脚本?

java - Java 8 Lambda 表达式是否使用 GPU?