简体   繁体   中英

Oracle db in UTF-16, Java hibernate webapp in UTF-8, ★ character not persisted

I have a webapp written in Java6 + Hibernate (jdbc oracle thin client) + Guice running on tomcat7 + ORACLE 10g. The application is fully encoded in UTF-8, the db is encoded in UTF-16.

From Oracle Docs I see that the client uses the jvm encoding to send data to the DB. The data is converted on the db and should be persisted correctly. Unfortunately when i try to persist a string containing this is persisted as a reversed question mark.

I tried to set the jvm parameter -Dfile.encoding=UTF8 , but this didn't work. Any other possible solution?

UPDATE :

Given an entity

@Entity
@Table(name="bean")
public class Bean{
    private String name;
    private String surname;
    //getters and setters
}

a service extending BasePersistentService which allows data access, a "bean" table with two columns:

name nvarchar2(200),
surname varchar2(200)

The exact use case is as follows:

// context initialized, BeanService injected in the variable beanService
Bean p = new Bean();
p.setName("myLittleStar★");
p.setSurname("nope");
beanService.save(p);

then on the db:

select * from bean;

result:

name: 'myLittleStar¿'
surname: 'nope'

This is the single jdbc resource

<Resource name="jdbc/dbConnPool" auth="Container"
    factory="oracle.ucp.jdbc.PoolDataSourceImpl" type="oracle.ucp.jdbc.PoolDataSource"
    description="Connection Pool DB" connectionFactoryClassName="oracle.jdbc.pool.OracleDataSource"
    initialPoolSize="10" minPoolSize="10" maxPoolSize="90" maxStatements="100"
    connectionWaitTimeout="30" inactiveConnectionTimeout="20"
    abandonedConnectionTimeout="600" user="*******" password="*******"
    url="*******" fastConnectionFailoverEnabled="true"
    onsConfiguration="*******" connectionPoolName="UCPPool"
    validateConnectionOnBorrow="true" />

This is the relevant part of the hibernate cfg

<session-factory>
    <property name="hibernate.search.default.directory_provider">
        org.hibernate.search.store.FSDirectoryProvider
    </property>

    <property name="connection.datasource">java:comp/env/jdbc/dbConnPool</property>
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="archive.autodetection">class, hbm</property>

</session-factory>

I have not found possible configurations to setup encoding in file.

Moreover I have found that it is not possible to configure the encoding in the oracle connection string.

Tell me if I can provide more useful data.

EDIT 2 : Replaced the type of the column 'name' in the 'bean' table from varchar2 to nvarchar2, this does not solve the problem but is necessary.

Horrible but working solution:

Encode the string that needs to be persisted using urlencode, Base64 or the encoding algorithm that better suits your needs. Of course the encoding must produce an output that does not contain characters that can't be persisted on your database.

You can either wrap your entity or change the setter. On getting the entity from the db you must decode the string the other way round.

This is an ugly workaround, but it fixed the problem locally .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM