I need to convert between a Map and a JSON string when communicating between a Java application and MySql. I've come across two very promising solutions: AttributeConverter and UserType.
Is there any pros/cons between choosing one solution over another? With all things considered equal, AttributeConverter sure does seem a lot simpler.
AttributeConverter
requires JPA 2.1 (Hibernate 4.3+), but if it's available, it's a much cleaner choice. A custom UserType may break with future versions of Hibernate, while an AttributeConverter likely won't.
Make sure that you specify the @Convert
annotation on the specific fields that you want to convert, rather than setting autoApply
, since you don't want to convert all Maps or all Strings.
Also make sure that you use a library for the Map-String conversion, rather than hand-coding it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With