3

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
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Post the myHomeDao.attachDirty code please – Kennedy Oliveira Apr 01 '15 at 13:17
  • That was auto generated by hibernate code generation process and is working fine for other columns. Code - `Transaction trans=sessionFactory.getCurrentSession().beginTransaction(); sessionFactory.getCurrentSession().saveOrUpdate(instance); trans.commit();` – sjain Apr 01 '15 at 13:20
  • Had a similar exception with `postgresql` using `Scala`'s Play framework `anorm`; posted my [solution here](https://stackoverflow.com/a/75342286/1159167) – Ricardo Feb 04 '23 at 00:50

1 Answers1

1

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!

Kennedy Oliveira
  • 2,141
  • 2
  • 20
  • 25
  • I think you are right although it seems odd that a database like `Postgre` can have such a bug when you just need to store a json and you find no way to do so in normal hibernate way then to write a custom query. Anyway I found the hack to write a update query instead of saveOrUpdate default hibernate way which worked. – sjain Apr 02 '15 at 13:20
  • @MyGod, yeah its really odd, i looked around for a solution and nothing like a correction for the bug, need to do workaround, anyway, i'm glad to help! – Kennedy Oliveira Apr 02 '15 at 13:23