在jpa hibernate SQL Server中将float / int转换为日历 [英] Convert float/int to Calendar in jpa hibernate SQL Server

查看:90
本文介绍了在jpa hibernate SQL Server中将float / int转换为日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA / Hibernate和SQL Server DB开发Java项目。

我得到一个存在的数据库,其中日期字段以int和datetime存储为float 。

有谁知道我该如何在我的域类中实现对Calendar类型的透明转换?



我必须在getter和setter中计算日期,还是有更好的方法?



谢谢!
Marco

解决方案

我会这样做:


  1. 两个类,MyDate&每个实现Hibernate的MyDateTime 用户类型 界面。 c> nullSafeGet()实现时,它会使用将该字符串转换为Joda DateTime或 Java.util.Date SimpleDateFormat 类,格式为yyyyMMdd。
  2. MyDateTime将读取 float 作为 String ,假定 float 的格式为20120828.190800。在 nullSafeGet()实现中,它也会将此字符串转换为Joda日期时间或使用 java.util.Date SimpleDateFormat 类,格式为yyyyMMdd.HHmmss。
  3. nullSafeSet()将在相应的类中使用相同的 SimpleDateFormat 格式化数据。



希望这有助于。


I'm developing a Java project with JPA / Hibernate and SQL Server DB.

I got an existent db where date fields are stored as int and datetime as float.

Does anyone know how can I implement a transparent conversion to a Calendar type in my domain class?

I must calculate the date in getter and setter or there's a better way?

Thank you! Marco

解决方案

I would do it as follows:

  1. Two classes, MyDate & MyDateTime each implementing the Hibernate UserType interface.
  2. MyDate will read the integer date as String, assuming the integer is of the format 20120828. In the nullSafeGet() implementation, it will convert this string to a Joda DateTime or Java.util.Date using the SimpleDateFormat class with format as "yyyyMMdd".
  3. MyDateTime will read the float as String, assuming the float is of the format 20120828.190800. In the nullSafeGet() implementation, it too will convert this string to a Joda Datetime or java.util.Date using the SimpleDateFormat class with format as "yyyyMMdd.HHmmss".
  4. The nullSafeSet() will format back the data using the same SimpleDateFormat in the respective classes.

Hope this helps.

这篇关于在jpa hibernate SQL Server中将float / int转换为日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆