Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JPA class converters

I'm using EclipseLink in Glassfish with my JavaEE application and have some java.util.Locale-columns in my model classes which I would like to store as String-columns in my database tables.

I know Hibernate ships it's conversion annotations and I can build my own converter implementing org.eclipse.persistence.mappings.converters.Converter. However, I have to depend on either of those libraries to use them.

Is there a way to get this functionality without directly depending on EclipseLink or Hibernate and staying with JPA spec?

like image 752
samy-delux Avatar asked Dec 17 '22 11:12

samy-delux


1 Answers

JPA doesn't provide that feature :(... in fact, it's feature base is very, very basic. You can achieve the same by creating a virtual attribute, so you have a String field in your class, which is mapped, and then you create a virtual attribute with a setLocale(Locale) and Locale getLocale(), which transform the string into a Locale and return it or extract the string representation of the locale.

I know it's not WOW, but it's the only way to avoid using EclipseLink or Hibernate custom annotations.

like image 145
Augusto Avatar answered Dec 19 '22 01:12

Augusto