We have a big table with a lot of columns. After we moved to MySQL Cluster, the table cannot be created because of:
ERROR 1118 (42000): Row size too l
As I explained in this article, the JPA AttributeConverter
is way too limited to map JSON object types, especially if you want to save them as JSON binary.
You don’t have to create a custom Hibernate Type to get JSON support, All you need to do is use the Hibernate Types OSS project.
For instance, if you're using Hibernate 5.2 or newer versions, then you need to add the following dependency in your Maven
pom.xml
configuration file:
com.vladmihalcea hibernate-types-52 ${hibernate-types.version}
Now, to explain how it all works.
As I explained in this article, for MySQL, you need to send the JSON object in a text form, so you need to use the JsonStringType
provided by the Hibernate Types project.
Now, you need to declare the new type on either at the entity attribute level, or, even better, at the class level in a base class using @MappedSuperclass:
@TypeDef(name = "json", typeClass = JsonStringType.class)
And the entity mapping will look like this:
@Type(type = "json")
@Column(columnDefinition = "json")
private Location location;
If you're using Hibernate 5.2 or later, then the JSON
type is registered automatically by MySQL57Dialect
.
Otherwise, you need to register it yourself:
public class MySQLJsonDialect extends MySQL55Dialect {
public MySQLJsonDialect() {
super();
this.registerColumnType(Types.JAVA_OBJECT, "json");
}
}
And, set the hibernate.dialect
Hibernate property to use the fully-qualified class name of the MySQLJsonDialect
class you have just created.