Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes Oracle Hibernate encoding problems with Spanish characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Oracle Hibernate encoding problems with Spanish characters" Watch "Oracle Hibernate encoding problems with Spanish characters" New topic
Author

Oracle Hibernate encoding problems with Spanish characters

Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
Since some days ago I’ve been trying to solve the following problems:

In a Web application Spanish characters like don't display the right way.

I’m using Hibernate 3.2.5, JSF 2.1, Java EE 5, Tomcat 6, Oracle 10g.

In the client



In the hibernate.cfg.xml



Seeking for solutions I tried to change the Oracle NLS_CHARACTERSET WE8ISO8859P1 to NLS_CHARACTERSET AL32UTF8 but it didn't work.

Now I notice a weird behavoir: When a client data is update using ajax the Spanish character are saved correctly, but when all the form is updated is saved the wrong way. I don't how to do. If someone can help me I'd be very grateful.




Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Does this help?
http://www.mularien.com/blog/2007/12/11/solving-problems-with-international-utf-8-data-using-hibernate-oracle-and-tomcat/


[How To Ask Questions][Read before you PM me]
Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
Additional information.
Apache configuration server httpd.conf has: AddDefaultCharset UTF-8
Java version 1.6



The above image shows an example with the word 'boy' in Spanish.

=====================================
P.S: English isn't my mother tongue.
Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
Hello Bill Gorder

Thank you for your reply. I read that article before and I added to my Hibernate configuration file:



I thought the following file was needed and I added too:



Needless to say it is not working. Thanks again.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

I believe the article also talked about data types and adding a property to the datasource in your context.xml. However that is a moot point since it seems you say the data is being stored correctly in the database. Is it also correct when you read it out of the database? Set a break point and check it after reading it out of the database (before the browser gets it) and see if it is still correct. If it is the problem is not with Hibernate.
Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
Hello again Bill Gorder

Is it also correct when you read it out of the database?

yes, it's.


So the problem is when sending the data to the database. What should I check? the server?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Have a look at this article: Character Conversions from Browser to Database.
Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
- Hello everybody:

Finally after trying out various means or theories by trial and error I got the following solution:

I created a filter to encode the character set for response/request. In the request was the problem since the browser (or server?) was changing the enconding when updating the form. No change needed to the database.



But in the web.xml



To be honest I don't undestrand very well this solution but was the only one I found. I hope it doesn't bring me side effects.

Thank you for all your help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That looks right. But just one comment: since you are hard-coding "UTF-8" into the Java code of your filter, that means that you don't need to specify it as an init-param of the filter. You aren't using that init-param in the code so you really ought to delete it from the web.xml file.

(Note: I'm not complaining about hard-coding "UTF-8" into the Java code. I did the same thing myself. I'm just complaining about the unnecessary parameter in the web.xml. And even that's a tiny tiny complaint.)

Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93


since you are hard-coding "UTF-8" into the Java code of your filter, that means that you don't need to specify it as an init-param of the filter.

Yes Paul, you are absolutely right!

The little problem I didn't mention is that I was trying to catch the parameter ("encoding" from web.xml) in the overriding init method of SetCharacterEncodingFilter class but it didn't work so, when I hard-coded "UTF-8" and I saw the right result I felt so exciting than I couldn't help to tell the world about it. Thanks for your cooperation and I will heed your advice.

====================================
P.S. English is not my mother tongue.

 
wood burning stoves
 
subject: Oracle Hibernate encoding problems with Spanish characters