1

I have a table with a column of type json in my postgreSQL DB (9.2). I'm using Hibernate and implemented the code example from Mapping postgreSQL JSON column to Hibernate value type suggested by Tim Fulmer, But I keep getting the following exception :

ERROR: column "jsonobject" is of type json but expression is of type character varying
Hint: You will need to rewrite or cast the expression.

I don't want to cast the expression because it is not recommended. I getting this error when I'm using a table that was pre-generated and the column is of type json. When I'm changing Hibernate configuration to build the table for me, I don't get the exception and the value are inserted to the newly created table but the type of this column is 'character varying(255)' instead of JSON.

I'm bet that I missed something in my implementation but can't understand what. Where should I look ? I even tried to set breakpoints in my code but I don't reach them.

Thanks.

  • You'll need to use a custom Hibernate type, create a cast in PostgreSQL to allow implicit conversion, or cast at the SQL level. – Craig Ringer Jan 7 '14 at 4:45
  • Show your mappings. At a guess, you've added the type support code, but have not actually used it in your Hibernate mappings. – Craig Ringer Jan 7 '14 at 4:47
  • The column mapping : <property name="someobjects" type="java.lang.String"> <column name="jsonobject" /> I tried to change the mapping but got exceptions that the type that I used is not known. – sagi Jan 7 '14 at 5:09
  • I think you'll need to specify typeClass= or similar. I don't work with Hibernate XML mappings, but you should be able to translate the annotations in the example given on the other answer. – Craig Ringer Jan 7 '14 at 5:47
  • I've added @TypeDefs( {@TypeDef( name= "StringJsonObject", typeClass = StringJsonUserType.class)}) at the top of my class (as mentioned in the other answer), but it does not help. I don't think that the usertype class is even being called. You suggest that I should put the typeClass in my xml mapping file ? – sagi Jan 7 '14 at 5:52
1

Found the solution, the following definition was really missing from my project. In the xml mapping file for the table I had to use type="com.example.StringJsonUserType" for that specific field instead of type="java.lang.String" so the mapping for this column is :

<property name="someobjects" type="com.example.StringJsonUserType">
    <column name="jsonobject" />
</property>

Sagi.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.