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() {
    }
}
Funsaized
  • 1,972
  • 4
  • 21
  • 41
  • 2
    Elaborate on "working with". – Rick James Mar 26 '18 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 http://mvnrepository.com/artifact/com.mopano/hibernate-json-contributor/1.0 Otherwise, you'll just have to use manual conversion or try and hope that a JPA AttributeConverter works. – coladict Mar 28 '18 at 07:02

1 Answers1

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?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Alex Chernyshev
  • 1,719
  • 9
  • 11
  • 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? – Daniel Patrick Apr 23 '19 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 https://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html . Will be good to see an error trace actually to be more specific. – Alex Chernyshev Apr 24 '19 at 16:31