JPA + Hibernate. Different column type for production and testing environment

Viewed 664

I have gone through all the existing questions on stackoverflow, although those questions looks similar, but none answer solved the problem, and the question is not the repeated one.

In production we are planning to use PostgreSQL, and for testing environment H2. We have models

 @TypeDefs({@TypeDef(name = "json", typeClass = JsonStringType.class)})
  @Entity
  @Table(name = "consumer_details")
  public class ConsumerDetails implements Serializable {
  private String name;
  @Type(type = "json")
  private ConsumerRequest info;
 }

 public class ConsumerRequest implements Serializable {
   private String clientName;
 }

Postgres supports data type Json and Jsonb. We are saving ConsumerRequest as column named "info" with data type as Jsonb in ConsumerDetails table. To support Json, we overiden Postgres dialect as

  public class CustomPostgresqlDialect extends PostgreSQL94Dialect {
    public CustomPostgresqlDialect() {
      super();
      this.registerColumnType(Types.JAVA_OBJECT, "jsonb");
    }
}

This works fine for prod environment backed by Postgres. But does not work for testing environment in H2 DB as H2 DB does not support json data type. I tried overriding H2Dialect as

public class CustomH2Dialect extends H2Dialect {
   public CustomH2Dialect() {
      super();
      this.registerColumnType(Types.LONGVARCHAR, "json");
   }
}

But it does not work and I gets exception as

WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 90026, SQLState: 90026
[http-nio-auto-1-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - 
Serialization failed, cause: "java.io.NotSerializableException: 
com.fasterxml.jackson.databind.node.ObjectNode" [90026-196]

Any idea how to solve this for testing environment.

0 Answers
Related