mysql - Spring数据JPA、Hibernate、MySQL5.7.13 : cannot find entity with chinese character

标签 mysql hibernate ubuntu spring-data

我有一个 Spring 应用程序,它从本地计算机 Ubuntu 14.04 上的 mysql 5.7.13 获取数据。

尝试获取包含中文字符的条件是行不通的。

我已经将架构和表设置为 utf8_default 排序规则和 InnoDB 引擎。

我在 Windows 下尝试了相同的应用程序,一切都运行良好,所以我只是不明白为什么它在 Ubuntu 中停止运行。

@Entity
@Table(name = "item")    
Item.java
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @NotEmpty()
    @Column(nullable = false, unique = true)
    private String name;
}

@Transactional(readOnly=true)
public interface ItemRepository extends JpaRepository<Item, Long>{
    Item findByName(String name);   
}

DBConfig.java {
        @Bean
public EntityManagerFactory entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);

    factory.setDataSource(dataSource());
    factory.setPackagesToScan("com.example.domain");
    factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);

    Properties jpaProperties = new Properties();
    jpaProperties.put("hibernate.dialect",
            "org.hibernate.dialect.MySQL5InnoDBDialect");
    jpaProperties.put("hibernate.hbm2ddl.auto", "validate");
    jpaProperties.put("hibernate.show_sql", "true");
    jpaProperties.put("hibernate.format_sql", "true");
    jpaProperties.put("hibernate.connection.CharSet", "utf8");
    jpaProperties.put("hibernate.connection.characterEncoding", "utf8");
    jpaProperties.put("hibernate.connection.useUnicode", "true");
    factory.setJpaProperties(jpaProperties);

    factory.afterPropertiesSet();
    return factory.getObject();
}
}

我尝试运行 is findByName(String name) ,当我传递“toto”时,我得到了该项目,当我传递“世界”时,它只返回 null。

根据日志,hibernate执行如下操作:

Hibernate: 
select
    item0_.id as id1_4_,
    item0_.name as name2_4_,

from
    item item0_ 
where
    item0_.name=?

等待解决...非常感谢

最佳答案

我自己没有尝试过,但你可以尝试通过 url 设置编码属性,例如:

?useUnicode=yes&characterEncoding=UTF-8

也许这个链接会有帮助:

http://mysql.rjweb.org/doc.php/charcoll

关于mysql - Spring数据JPA、Hibernate、MySQL5.7.13 : cannot find entity with chinese character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37623931/

相关文章:

mysql - 从 Hibernate/MySQL 获取数据性能

ubuntu - 本地主机连接由同行重置,运行 angularjs webpack 应用程序的 docker 容器(ubuntu)

MySQL根据两列对数据进行分类

php - 使用 MySQL 编写复杂查询时遇到问题

mysql - 尝试在 MySQL 中定位和批量编辑 WooCommerce 价格

php - 使用 MySQL,如何选择特定行的查询结果排名?

java - 使用 Hibernate 的 Java Web 服务中的内存泄漏

hibernate - 找不到神器 hibernate

ubuntu - 无法在 Ubuntu 上运行 qemu,即使是一个简单的 guest

ubuntu - 从外部 IP 地址访问主机