【问题标题】:Cannot store "Pile of Poo" unicode emoji using Spring Boot Hibernate and MySQL无法使用 Spring Boot Hibernate 和 MySQL 存储“Pile of Poo”Unicode 表情符号
【发布时间】:2015-08-21 01:57:39
【问题描述】:

在运行 Spring Boot JPA 示例时:https://spring.io/guides/gs/accessing-data-jpa/ 针对 MySQL 数据库并尝试存储“便便堆”表情符号????我收到异常 java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xA9\xF0\x9F...'

我将我的数据库配置为使用 utf8mb4 编码。

我知道这不是 MySQL 的问题,因为我可以使用 MySQL 客户端创建客户并将其存储在数据库中。我什至可以运行示例应用程序并让它找到带有“一堆便便”表情符号的客户。

2015-06-05 18:10:12.382  INFO 5119 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@446b0224: startup date [Fri Jun 05 18:10:12 PDT 2015]; root of context hierarchy
2015-06-05 18:10:13.567  INFO 5119 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2015-06-05 18:10:13.588  INFO 5119 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2015-06-05 18:10:13.648  INFO 5119 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.8.Final}
2015-06-05 18:10:13.649  INFO 5119 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2015-06-05 18:10:13.651  INFO 5119 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2015-06-05 18:10:13.804  INFO 5119 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-06-05 18:10:14.086  INFO 5119 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2015-06-05 18:10:14.171  INFO 5119 --- [           main] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2015-06-05 18:10:14.365  INFO 5119 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2015-06-05 18:10:14.365  INFO 5119 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000102: Fetching database metadata
2015-06-05 18:10:14.366  INFO 5119 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000396: Updating schema
2015-06-05 18:10:14.390  INFO 5119 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: poo_test.Customer
2015-06-05 18:10:14.390  INFO 5119 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [id, lastname, firstname]
2015-06-05 18:10:14.390  INFO 5119 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: []
2015-06-05 18:10:14.391  INFO 5119 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [primary]
2015-06-05 18:10:14.391  INFO 5119 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000232: Schema update complete
2015-06-05 18:10:14.777  INFO 5119 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
Customer found with findOne(1L):
--------------------------------
Customer[id=1, firstName='????????????????', lastName='????????????????']

2015-06-05 18:10:14.836  INFO 5119 --- [           main] hello.Application                        : Started Application in 2.819 seconds (JVM running for 3.077)
2015-06-05 18:10:14.837  INFO 5119 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@446b0224: startup date [Fri Jun 05 18:10:12 PDT 2015]; root of context hierarchy
2015-06-05 18:10:14.838  INFO 5119 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2015-06-05 18:10:14.839  INFO 5119 --- [       Thread-1] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

这是我从示例中修改的 Application.java:

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    CustomerRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Override
    public void run(String... strings) throws Exception {
        // save a couple of customers
        repository.save(new Customer("????????????????", "????????????????"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer by ID
        Customer customer = repository.findOne(1L);
        System.out.println("Customer found with findOne(1L):");
        System.out.println("--------------------------------");
        System.out.println(customer);
        System.out.println();

    }

}

这是我用来配置我的 JDBC 连接的 application.yml 文件。我使用了许多在研究此问题时发现的技术,但似乎没有任何效果。

spring:

  datasource:
    url: "jdbc:mysql://localhost:3306/poo_test?useUnicode=yes&characterEncoding=utf8&characterResultSets=utf8"
    username: root
    password: 
    connectionInitSqls: "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
    driverClassName: com.mysql.jdbc.Driver
    testOnBorrow: true
    validationQuery: SELECT 1

  jpa:
    show-sql: false
    database-platform: org.hibernate.dialect.MySQL5Dialect
    hibernate:
      ddl-auto: update
      naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
      connection:
        CharSet: utf8mb4
        characterEncoding: utf8
        useUnicode: true

这似乎是一个常见问题,但我还没有找到解决方案。任何帮助将不胜感激。

【问题讨论】:

    标签: mysql hibernate spring-boot spring-data-jpa emoji


    【解决方案1】:

    在尝试了各种方法后,我设法使用以下 application.yml 让事情正常进行

    spring:
    
      datasource:
        url: "jdbc:mysql://localhost:3306/poo_test?useUnicode=yes&characterEncoding=utf8"
        username: root
        password: 
        initSQL: "SET NAMES 'utf8mb4'"
    
      jpa:
        hibernate:
          ddl-auto: update
    

    诀窍似乎是 initSQL 属性

    【讨论】:

    • 更好用spring.datasource.init-sql="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
    • 目前是spring.datasource.tomcat.initSQL = SET NAMES 'utf8mb4'
    【解决方案2】:

    我做了同样的事情但它对我不起作用,搜索后我发现我必须编辑mysql的my.cnf文件

    sudo nano /etc/mysql/my.cnf
    

    然后在mysqld下添加这一行

    character_set_server=utf8mb4
    

    然后重启mysql

    sudo /etc/init.d/mysqld restart
    

    注意:如果您使用的是谷歌云,您应该添加标志并从控制台重新启动 mysql,因为 mysql 与您的代码不在同一台服务器上。

    【讨论】:

      【解决方案3】:

      就我而言,以下链接解决了我的问题 Mathias mysql

      特别是:

      [client]
      default-character-set = utf8mb4
      
      [mysql]
      default-character-set = utf8mb4
      
      [mysqld]
      character-set-client-handshake = FALSE
      character-set-server = utf8mb4
      collation-server = utf8mb4_unicode_ci
      

      【讨论】:

        猜你喜欢
        • 2015-08-18
        • 1970-01-01
        • 2018-12-14
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 1970-01-01
        相关资源
        最近更新 更多