Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeConverter vs UserType for Hibernate

Tags:

hibernate

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.

like image 305
kennyg Avatar asked Jun 21 '16 23:06

kennyg


1 Answers

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.

like image 72
ThrawnCA Avatar answered Oct 21 '22 02:10

ThrawnCA