Search code examples
javahibernatepostgresqlgsonrestlet

Updating a column value of type json failed with an exception


I am using postgresql and one of my column is of type json.

I am using REST API and the response that I get is a json string which I am storing in that json column.

Using Hibernate, I am not able to do a update for this column.

NOTE: I am using google Gson to make it a json and store it to db.

Example:

Gson gson = new Gson();
myBean.setJsonData(gson.toJson(response));
myHomeDao.attachDirty(myBean); //Error Here

LOG:

Caused by: org.postgresql.util.PSQLException: ERROR: column json_data" is of type json but expression is of type bytea
  Hint: You will need to rewrite or cast the expression.
  Position: 247
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:365)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 16 more

Solution

  • It looks like a bug from Postgre, i found this workaround that you can use to solve your problem maybe.

    If it can't help, maybe you'll need to write a custom hibernate tybe that use the setObject method on jdbc drivers instead of using text or byte.

    Hope it helps!