mysql - 在 Google App Engine 开发环境中使用 JPA 时,非 Ascii 字符保存为 '?'

标签 mysql google-app-engine jpa eclipselink google-cloud-endpoints

我正在使用 JPA(EclipseLink 实现)在 Google Cloud SQL 之上构建 Google Cloud Endpoints API。 当我通过该 API 插入具有非 ascii 字符(即希伯来语或阿拉伯语)的字符串时,它在 Google App Engine 上部署的应用程序中工作正常,但在我本地的 Java 开发服务器上,这些字符保存为“?”。 当我使用“常规”servlet 访问相同的数据库表并插入相同的字符串(使用 DriverManager.getConnection() 获取数据库连接)时,一切正常,因此我的本地数据库设置不会有问题。

我使用(用于 servlet 和基于 JPA 的实现)“com.mysql.jdbc.Driver”,我的连接 URL 是

"jdbc:mysql://127.0.0.1:3306/guestbook?user=root&useUnicode=true&characterEncoding=UTF-8"

我的 appengine-web.xml 文件中有以下条目:

<property name="file.encoding" value="UTF-8" />
<property name="DEFAULT_ENCODING" value="UTF-8" />

并且 character_set_server 和 character_set_database 在我的本地服务器上都设置为 utf8。

这是我将字符串插入数据库的代码:

public class Guestbook {
private static     Map<String, String> properties; 
private static     EntityManagerFactory emf;

static{
    properties = new HashMap();

      properties.put("javax.persistence.jdbc.driver",
          "com.mysql.jdbc.Driver");
      properties.put("javax.persistence.jdbc.url",
             "jdbc:mysql://127.0.0.1:3306/guestbook?user=root&amp;useUnicode=true&amp;characterEncoding=UTF-8");

    emf = Persistence.createEntityManagerFactory(
        "Demo", properties);

}
@ApiMethod(name = "greetings.insert", httpMethod = "post")
  public void insertEntry(GuestEntry entry) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(new GuestEntry(entry.getMessage()));
        em.getTransaction().commit();
        em.close();
  }
}

有人知道吗?

最佳答案

Afaik,只有开发服务器控制台无法显示 UTF-8 字符。否则数据保存得很好。

关于mysql - 在 Google App Engine 开发环境中使用 JPA 时,非 Ascii 字符保存为 '?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163891/

相关文章:

mysql - 将 select result_id 替换为 MySQL 中的名称

java - JSP 文件未在 AppEngine 中运行的 Spring Boot 中呈现

java - eclipse链接 2.7.0 : Static weaving error for "javax.persistence.AttributeConverter"'s signer information does not match

google-app-engine - Google App Engine - 默认 MIME 类型

Spring Boot 访问 H2 控制台

java - Spring Boot - 实体中的自定义类字段

python - 为什么 'insert' 函数不使用 MySQLdb 添加行?

MySQL "IN"使用子查询查询非常慢,但使用显式值查询速度很快

PHP/MySQL : Why is my database all of the sudden case-sensitive for table names on my Linux server (and not on my XP server)?

java - 如何在基于 GWT 的项目中包含本地 Java 程序?