-->
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.  [ 3 posts ] 
Author Message
 Post subject: JdbcTypeJavaClass: class.hashCode() as special code ?
PostPosted: Tue Jan 24, 2017 5:25 pm 
Newbie

Joined: Mon Jan 23, 2017 5:02 pm
Posts: 2
Context: Not a hibernate expert, working with postgres 9.4+ jsonb fields. When doing parameterized update with plain jdbc I can simply create a org.postgresql.util.PGobject and pass it as a parameter to PreparedStatement.setObject. It might be not reasonable to expect similar behavior from Hibernate, but the current behavior leaves me puzzled:

When a AttributeConverter<Map, Object> is used to build a similar PGobject, the following exception is raised by the postgres jdbc driver:

Code:
org.postgresql.util.PSQLException: Unsupported Types value: 1.029.991.479


and the source of this type value can be traced to the following method of the JdbcTypeJavaClassMappings:

Code:
   public int determineJdbcTypeCodeForJavaClass(Class cls) {
      ....
      int specialCode = cls.hashCode();
      log.debug(
            "JDBC type code mapping not known for class [" + cls.getName() + "]; using custom code [" + specialCode + "]"
      );
      return specialCode;
   }


My question is: why to take the hashcode of the class of the attribute and use it as an "unknown" java.sql.Types value? Is there a good explanation for this seemingly random choice? If so, wouldn't it be nice to add it as a comment line, or better yet as a test?

If the "special code" is changed say to java.sql.Types#OTHER, then no test of the hibernate-orm project fails and my mapping via org.postgresql.util.PGobject to jsonb starts to work. I would have expected this or some other constant to be used instead of random hash code that can (theoretrically) clash with other predefined values.

Reproduced: Hibernate 5.2.6.Final and 5.2.7.Final, jdbc driver org.postgresql:postgresql:9.4.1212.jre7, PostgreSQL 9.5.5 via docker

A small demo is available at https://github.com/mgurov/pghibernatejsonb

Please let me know if I can provide more info on the matter


Top
 Profile  
 
 Post subject: Re: JdbcTypeJavaClass: class.hashCode() as special code ?
PostPosted: Wed Jan 25, 2017 2:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Using JSON via AttributeConverter is not very suitable. You should use Generic Types as explained in this article so you can better control the JDBC Type and also be able to map to a String or a Binary PostgreSQL JSON column type.

The goal of that hashCode specialCode is an attempt to guess the underlying JDBC Type when the javaClassToJdbcTypeCodeMap does not return a proper int code for the incoming String-based one.
That's why I said that using AttributeConverter for exotic types is not recommended.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: JdbcTypeJavaClass: class.hashCode() as special code ?
PostPosted: Thu Jan 26, 2017 5:25 pm 
Newbie

Joined: Mon Jan 23, 2017 5:02 pm
Posts: 2
@vlad, thank you very much for the explanation.

Sad that such a heavy-weight machinery is needed to utilize very useful database types like the jsonb of postgres, but I guess there is no easy way to simplify this in a way generic enough.

I should admit that I still do not see how exactly the use of Class.hashCode() helps with guessing the underlying JDBC type, but i leave this to your judgement whether this approach is reasonable or not.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.