我有一个 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
也许这个链接会有帮助:
关于mysql - Spring数据JPA、Hibernate、MySQL5.7.13 : cannot find entity with chinese character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37623931/