Java JPA 类转换器
我在 Glassfish 中使用 EclipseLink 和我的 JavaEE 应用程序,并且在我的模型类中有一些 java.util.Locale 列,我希望将它们作为字符串列存储在我的数据库表中。
我知道 Hibernate 提供了它的转换注释,我可以构建自己的转换器来实现 org.eclipse.persistence.mappings.converters.Converter。但是,我必须依赖这些库中的任何一个才能使用它们。
有没有一种方法可以在不直接依赖 EclipseLink 或 Hibernate 并遵守 JPA 规范的情况下获得此功能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JPA 不提供该功能:(...事实上,它的功能基础非常非常基本。您可以通过创建虚拟属性来实现相同的目的,因此您的类中有一个已映射的 String 字段,并且然后您使用 setLocale(Locale) 和 Locale getLocale() 创建一个虚拟属性,它将字符串转换为 Locale 并返回它或提取区域设置的字符串表示形式
我知道这不是 WOW,但这是避免的唯一方法。使用EclipseLink 或 Hibernate 自定义注释。
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 引入了 AttributeConverter 和 @Converter 注释。
我与支持它的 DataNucleus 合作。 EclipseLink 支持似乎正在开发中。我不知道休眠。
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.