开发者

Java JPA class converters

开发者 https://www.devze.com 2023-03-19 23:23 出处:网络
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'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. Howeve开发者_如何学Pythonr, 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?


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.


JPA 2.1 introduces AttributeConverter and @Converter annotation.

I worked with DataNucleus which supports it. And EclipseLink support seems to be in development. I don't know about Hibernate.

0

精彩评论

暂无评论...
验证码 换一张
取 消