kftse
kftse

Reputation: 191

Storing unicode characters using JPA in Oracle 11g

I am having a problem storing unicode characters to Oracle 11g using JPA. When I store a unicode string to DB using this (someTranslation is an object containing unicode strings).

someTranslation.setMessage("文字"); //literally means characters
em.persist(someTranslation);

The string is then displayed as an inverted '?' ("¿")

When I manually fix a corrupted string in DB using SQLDeveloper, the characters are then displayed correctly as what it is intended.

I've searched a bit that when using MySQL, only a small modification is needed to correct this. I feel I am having the same problem with that post except I am using oracle 11g instead. Do anyone know of a solution for this problem in oracle 11g? Thanks in advance.

Here is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="cbpejb" transaction-type="JTA">
    <class>entity.Translation</class>
    <properties>
        <property name="openjpa.ConnectionDriverName" value="oracle.jdbc.OracleDriver" />
        <property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:orcldb" />
        <property name="openjpa.ConnectionUserName" value="dev" />
        <property name="openjpa.ConnectionPassword" value="dev00" />
        <property name="openjpa.jdbc.Schema" value="DEV" />
    </properties>
</persistence-unit>

Upvotes: 3

Views: 2103

Answers (1)

gerrytan
gerrytan

Reputation: 41113

There are two possible problems:

  1. You've selected wrong database column type (eg: db column / configuration did not support multibyte characters). In Oracle you need to use NCHAR / NVARCHAR
  2. You selected correct column type, but you did not display it properly.

As of Java itself, the String class supports Unicode, and uses UTF-16 encoding, but your JDBC driver should handle en/decoding

Upvotes: 2

Related Questions