9

What is the best practice for working with JSON column within Java code? Specifically I am interested in being able to save & query JSON columns in a MySQL DB using hibernate..

@Entity
public class MyEntity {

    private String myField; // this field is a json column

    public MyEntity() {
    }
}
2
  • 2
    Elaborate on "working with". Commented Mar 26, 2018 at 20:26
  • If you're using the JSR-353 javax.json classes, then there is one way to automate the conversion to and from strings by just adding this dependency to your project mvnrepository.com/artifact/com.mopano/… Otherwise, you'll just have to use manual conversion or try and hope that a JPA AttributeConverter works. Commented Mar 28, 2018 at 7:02

1 Answer 1

3

There is a good library to deal with JSON types in Hibernate. It's called Hibernate Types, and you can scroll down to the MySQL section to get the right example.

And some samples of usage MySQL's JSON API inside query:

How to search JSON data in MySQL?

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

2 Comments

I get exceptions when using this library with values that are set to null or objects that are nested more than one layer deep. Anyone have any ideas on that?
@DanielPatrick , this library is based on common Jackson library, so at least issue with null could be solved by proper Jackson mapper configuration fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/… . Will be good to see an error trace actually to be more specific.

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.