-->
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.  [ 6 posts ] 
Author Message
 Post subject: Oracle char with padding whitespace problem
PostPosted: Sun Aug 29, 2010 6:31 am 
Newbie

Joined: Sun Aug 29, 2010 5:37 am
Posts: 3
Hallo Hibernate-Community,

ich habe folgendes Problem: Ich benutze Hibernate 3.5.5, eine Oracle 10g Datenbank und den dazugehörigen JDBC-Treiber und möchte ein native SQL - Statement absetzen, z.B.
Code:
-- DB-Tabelle
create table mytable(
pk number primary key,
col1 char(5) not null)

// native SQL - Abfrage
"select count(*) from mytable where col1 = :param"
param = "abc" // liefert 0 zurück
param = "abc  " // liefert 1 zurück (Leerzeichenproblematik)

Leider wird auf der Datenbank immer char anstatt varchar2 verwendet und das kann ich leider nicht verändern, weil sonst unser anderes Legacy-System nicht mehr funktionieren würde. Ich habe im Internet gelesen, dass man im PreparedStatement setFixedChar aufrufen soll, was auch funktioniert (ich habe ein kleines JDBC-Beispielprogramm erstellt), allerdings weiß ich nicht wie ich Hibernate dazu bewegen kann setFixedChar anstatt setString aufzurufen.
Ich habe auch schon die Sourcen von Hibernate heruntergeladen und einen üblen Hack in StringType (Methoden set und nullSafeSet) eingebaut der funktioniert:
Code:
public void set(PreparedStatement st, Object value, int index) throws SQLException {
  if(st instanceof OraclePreparedStatement) {
     String strValue=(String)value;
     // Oracle behandelt leere Strings "" als Nullwerte
     // und lässt keinen Insert/Vergleich etc zu
     strValue = strValue.equals("") ? " " : strValue;

     ((OraclePreparedStatement)st).setFixedChar(index, strValue);
  } else {
     st.setString(index, (String) value);
  }
}

Wenn ich diesen Code in einen eigenen UserType und für alle Datenbanktabellen (automatisch) nutzen könnte oder wenn man dem Oracle JDBC-Treiber mitteilen könnte, dass er automatsich setFixedChar nutzt, dann wär mein Problem behoben.

Ich würde mich echt riesig freuen, wenn mir jemand weiterhelfen könnte.

Viele Grüße

Josef


Top
 Profile  
 
 Post subject: Re: Oracle char with padding whitespace problem
PostPosted: Mon Aug 30, 2010 2:46 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hi Josef,

After translating your passage , I understood its a problem passing white space if not stop reading this passage.

(String)value.trim()

gives "abc "-> "abc"

-----------------------
Hallo Josef,

Nachdem Ihren Durchgang übersetzend, habe ich verstanden, dass sein ein Problem, das an weißem Platz vorbeigeht, wenn aufhört nicht, diesen Durchgang zu lesen.

(String)value.trim()

Gibt "abc " -> "abc"

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Oracle char with padding whitespace problem
PostPosted: Mon Aug 30, 2010 5:17 am 
Newbie

Joined: Sun Aug 29, 2010 5:37 am
Posts: 3
Hi shynate26,

well the actual problem is that the Oracle JDBC driver converts the parameter to varchar2 and compares it with char.
Code:
e.g.
in database:       'abc  ' -- char(5)
query.setParameter("abc") -- java.lang.String

The comparison fails, because the parameter doesn't contain any whitespaces.
Another solution would be, if oracle would treat the parameter as a char-value instead of varchar2.

It would be great if I could change this behaviour in the JDBC settings or if I could tell hibernate to always use my own (User)Type (MyStringType extends StringType or MyOtherUserType implements UserType) for each query.

Do you know an alternative to this hibernate-specific statements, which would use my UserType as a default? It would be great if I could configure this via xml.

Code:
Configuration cfg = new Configuration();
SessionFactory sessionFactory =   cfg.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
org.hibernate.Query query = session.createSQLQuery("insert into dbtest3 values (95,:v)");
query.setParameter("v", "abc",new MyStringType()); // for every string


Best regards

Josef


Top
 Profile  
 
 Post subject: Re: Oracle char with padding whitespace problem
PostPosted: Tue Aug 31, 2010 1:00 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Josef,

If you have problem retriving the data, you can make use of trim(Column_name)=:your_Parameter.

If you always a space after the string while inserting/updating then you have to go for UserType. else string will do.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Oracle char with padding whitespace problem
PostPosted: Wed Sep 01, 2010 11:35 am 
Newbie

Joined: Sun Aug 29, 2010 5:37 am
Posts: 3
Thanks for the quick reply.

Can you please tell me how to use the UserType with JPQL or Native Queries (but not with Hibernate-Queries)?
For example there must be an option to use my UserType instead of Hibernate's one.
I'm looking for something like the dialect mechanism. I just have to declare via xml which one to use and Hibernate will use it. How can I do something similar for the UserType?

Best regards


Top
 Profile  
 
 Post subject: Re: Oracle char with padding whitespace problem
PostPosted: Thu Sep 02, 2010 1:47 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hi JOsef,

The user type as a Java file implementing the implements UserType, Serializable .

If you override these methods you can achieve !!

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

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.