2

I got a problem for retrieving a record with Chinese characters in Java. Here is my code:

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );
EntityManager entitymanager = emfactory.createEntityManager();

Query query = entitymanager.createQuery("Select e from Employee e where e.id = " + empId);
Employee employee = query.getSingleResult();

if (employee != null)
{
    System.out.println(employee.getChineseName());
    System.out.println(employee.getChineseAddress());
}

When the program executes the SQL statement, it pops the error: "java.sql.SQLException: Cannot map Oracle character to Unicode". If I retrieve a record without Chinese characters, the program works fine. My database is Oracle and the Chinese characters are in the GBK character set. How can I solve this problem?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Kenny
  • 21
  • 1
  • Have you tried a straight JDBC call, without all the JPA complications, as a test? – Basil Bourque Sep 09 '17 at 16:07
  • I find it a little difficult to believe there exists in Oracle a character set containing code points that do not exist in Unicode. Therefore, my strong suspicion is that somewhere, JPA is telling Oracle to interpret the data using the wrong character set. But I'm not familiar enough with JPA to tell you where to look. – Jim Garrison Sep 09 '17 at 16:24
  • You should be using both parameterized queries and generics. – chrylis -cautiouslyoptimistic- Sep 09 '17 at 16:30

0 Answers0