4

I am using Hibernate in SpringBoot to manage my DB layer. I have a json column in my Hibernate Entity:

    @Type(type = "json")
    @Column(columnDefinition = "json")
    private LibraryContent libraryContent;

I am using Postgres for production and H2 when running my tests using the @DataJPATest annotation.

To get the json column to work with postgres I had to add:

@TypeDef(name = "json", typeClass = JsonBinaryType.class)

However, when I run my tests with this I get:

Caused by: org.h2.jdbc.JdbcSQLDataException: Data conversion error converting "OTHER to JSON"

But when I change the @TypeDef typeClass to JsonStringType.class, the tests will all run, but I will get an error when running the application with Postgres saying that it cannot convert type json to varchar.

Is there another way to map the json column so that it works for H2 (testing) and Postgres (prod)?

Or is there a way to specify different @TypeDefs for different SpringBoot Profiles?

2
  • Did you manage to fix this? Commented Mar 15, 2021 at 13:21
  • I did not, I just manually switch the @TypeDef between JsonBinaryType and JsonStringType depending on the environment I'm using. Commented Mar 16, 2021 at 19:29

1 Answer 1

2

I added two package-info.java one in the tests folder with JsonStringType for h2 and another with JsonBinaryType for postgree

Sign up to request clarification or add additional context in comments.

2 Comments

Could you post a small example by chance?
On your model package-info.java: @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class). On your test package-info.java: @TypeDef(name = "jsonb", typeClass = JsonStringType.class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.