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".
    – Rick James
    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.
    – coladict
    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?

2
  • 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? Apr 23, 2019 at 14:10
  • @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. Apr 24, 2019 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.