-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 34 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Hibernate oracle unicode support
PostPosted: Wed Nov 12, 2003 1:25 am 
I am using hibernate 1.x with Oracle 9i. I need to store double byte data into the database and I prefer to use unicode. I have set the database the national character set to be "UTF8" and I defined several columns to have type: NVARCHAR2.

However, when I use hibernate to save the string, it ended with the "garbage" characters in the database. I saw some postings to set character encoding in the jdbc url, I did that and that did not solve the problem:

hibernate.connection.url=jdbc:oracle:thin:@csiceast:6660:sdd1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

I also did:

hibernate.connnection.charSet=UTF8

But that did not solve the problem either.

I am also aware that oracle jdbc drive has a method "setFormOfUse" for setting columns with NVARCHAR2 type. However, I don't know whether Hibernate implementation used it or somehow I can tell it to use it.

I would appreciate your help.


Top
  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 5:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I suggest you figure out how to solve this problem by using direct JDBC, and then do the same in Hibernate. This problem has nothing at all to do with Hibernate. You should have no problem making internationalized strings work on Oracle.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 9:31 am 
I did the direct jdbc with the instruction from Oracle. I did two tests: if i specify "setFormOfUse", i get the correct content in the database. However, it i did not, the content is not correct unicode.

The question is: whether hibernate oracle dialect takes this into account? i look at the source code, I could not find any calls to this method. Thus, my guess is that it did not.

If that is the case, my next question is: any suggestions on how to handle this problem - maybe make code changes to hibernate.

Also, keep in mind that i have set the database the national character set to be "UTF8", however Character set to be USASCII7.


Top
  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 9:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I'm quite confident you'll find a way to do this without changing Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 11:09 am 
Can you be more specific? I am waiting for the suggestions.


Top
  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 9:22 am 
Newbie

Joined: Wed May 26, 2004 4:16 am
Posts: 7
gavin wrote:
I'm quite confident you'll find a way to do this without changing Hibernate.


Oracle suggestion is quite clear
http://otn.oracle.com/sample_code/tech/java/codesnippet/jdbc/nls/Globalization.html

I wonder how people can use NVARCHAR2 and Hibernate's StringType without problems...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 2:47 pm 
Newbie

Joined: Fri Mar 26, 2004 8:44 am
Posts: 10
hi there,

I've faced the same problem right now.

I'll try to describe the problem in brief terms:
*) Oracle has special datatypes for supporting Unicode Characters: NCHAR, NVARCHAR, NCLOB
*) To correctly use them in insert or update statements, you have to use:
oracle.jdbc.OraclePreparedStatement ps = (oracle.jdbc.OraclePreparedStatement)conn.prepareStatement("INSERT ....");
ps.setFormOfUse(5, oracle.jdbc.OraclePreparedStatement.FORM_NCHAR);
ps.setString(5, "unicodestring\u0100");

==> Of course a possible solution would be to retrieve the Connection and do these statements manually.

However, the main question is, whether there is some way to integrate this type of access into Hibernate, as this problem will come across any developer wanting to use the unicode datatypes of Oracle (9i+10g).

Suggestions are perfectly welcome,
Johannes


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 23, 2004 3:42 am 
Newbie

Joined: Wed May 26, 2004 4:16 am
Posts: 7
[quote="joe_the_quick"]
However, the main question is, whether there is some way to integrate this type of access into Hibernate, as this problem will come across any developer wanting to use the unicode datatypes of Oracle (9i+10g).

Suggestions are perfectly welcome,
Johannes[/quote]

public class UTFStringType extends StringType{
public void set(PreparedStatement st, Object value, int index) throws SQLException {
if (st instanceof OraclePreparedStatement) {
((OraclePreparedStatement)st).setFormOfUse(index, OraclePreparedStatement.FORM_NCHAR);
}
super.set(st, value, index);
}
}

Use it instead of built-in string type.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 3:53 am 
Newbie

Joined: Fri Mar 26, 2004 8:44 am
Posts: 10
dear furman,

thank you very much for the really detailed response (I was on holidays so I didn't answer earlier)!!

what is the best way to register this custom type to hibernate to use it in the xml file?
(to be honest I haven't worked much with custom types yet).

thank you very much,
johannes


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 12:03 pm 
Newbie

Joined: Fri Mar 26, 2004 8:44 am
Posts: 10
The solution works smoothly, thank you very much!
I put it to work as follows:

<property name="ntitle" type="UTFStringType" >
<column name="NTITLE" sql-type="nvarchar(100)" not-null="true"/>
</property>

thx again
johannes


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 4:12 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
furman wrote:
Code:
public class UTFStringType extends StringType{
    public void set(PreparedStatement st, Object value, int index) throws SQLException {
        if (st instanceof OraclePreparedStatement) {
            ((OraclePreparedStatement)st).setFormOfUse(index, OraclePreparedStatement.FORM_NCHAR);
        }
        super.set(st, value, index);
    }
}


Use it instead of built-in string type.


Works as long as you are not using any connection pool behind the scenes... In this case the connection returned by the pool is unlikely to be an OraclePreparedStatement but a proxy instantiated by the pool.


Top
 Profile  
 
 Post subject: Trouble with Hibernate Datasource
PostPosted: Sun Sep 19, 2004 3:09 pm 
Newbie

Joined: Fri Mar 26, 2004 8:44 am
Posts: 10
I've tried to use a JNDI datasource with Hibernate, but this has raised a ClassCastException as you've already thought of.

hibernate.properties:
hibernate.connection.datasource java:/XAOracleDS

Code:
public void set(PreparedStatement st, Object value, int index) throws SQLException {
      
      OraclePreparedStatement orast = (OraclePreparedStatement)st;
      orast.setFormOfUse(index, OraclePreparedStatement.FORM_NCHAR);
      
      /*if (st instanceof OraclePreparedStatement) {
         ((OraclePreparedStatement)st).
      }*/
      
      super.set(orast, value, index);
   }



After using the datasource, the cast of the PreparedStatement fails with a ClassCastException.
Any ideas or does it mean I cannot use a DataSource with Hibernate?

thx alot
johannes


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 4:14 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
It means you can use DataSource without any problem but you cannot cast to the OracleStatement... Just because you are not supposed to make such assumption... and you are likely to have the same problem with nearly any kind of DataSource implementation or ConnectionPool mechanism.

Except maybe if you try the very latest version of Proxool (from CVS). The Connections and Statements served by Proxool should implement the vendor specific interfaces - you should then be able to cast it as you expect.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 5:40 pm 
Newbie

Joined: Fri Mar 26, 2004 8:44 am
Posts: 10
thank you for the fast response.

however, I need this assumption to allow for correct storage of Unicode-data into the Oracle database NVARCHAR field (otherwise the UTF8-data will not be stored correctly).

Is there a "general" way to use vendor-specific extensions (like OraclePreparedStatement.setFormOfUse) in Hibernate?
I thought the User-Datatypes have been created to serve exactly this need.

To be honest, I wouldn't like to use a pool implementation of a specific vendor (except if this is the only way to do it).
Would reflection be a technique of use here (I fear not, because it seems the Pool doesn't know it has been an OraclePreparedStatement anymore).

thx alot for your ideas,
johannes


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 5:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
User your debugger, take a look at the DataSource object, probably there is a way to get the original, wrapped statement from it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 34 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.