在jpa hibernate SQL Server中将float / int转换为日历 [英] Convert float/int to Calendar in jpa hibernate SQL Server
本文介绍了在jpa hibernate SQL Server中将float / int转换为日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用JPA / Hibernate和SQL Server DB开发Java项目。
我得到一个存在的数据库,其中日期字段以int和datetime存储为float 。
有谁知道我该如何在我的域类中实现对Calendar类型的透明转换?
我必须在getter和setter中计算日期,还是有更好的方法?
谢谢!
Marco
解决方案
我会这样做:
- 两个类,MyDate&每个实现Hibernate的MyDateTime
用户类型
界面。 c> nullSafeGet()实现时,它会使用将该字符串转换为Joda DateTime或
类,格式为yyyyMMdd。Java.util.Date
SimpleDateFormat
- MyDateTime将读取
float
作为String
,假定float
的格式为20120828.190800。在nullSafeGet()
实现中,它也会将此字符串转换为Joda日期时间或使用java.util.Date
SimpleDateFormat
类,格式为yyyyMMdd.HHmmss。
nullSafeSet()
将在相应的类中使用相同的SimpleDateFormat
格式化数据。
- MyDateTime将读取
希望这有助于。
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:
- Two classes, MyDate & MyDateTime each implementing the Hibernate
UserType
interface. - 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 orJava.util.Date
using theSimpleDateFormat
class with format as "yyyyMMdd". - MyDateTime will read the
float
asString
, assuming thefloat
is of the format 20120828.190800. In thenullSafeGet()
implementation, it too will convert this string to a Joda Datetime orjava.util.Date
using theSimpleDateFormat
class with format as "yyyyMMdd.HHmmss". - The
nullSafeSet()
will format back the data using the sameSimpleDateFormat
in the respective classes.
Hope this helps.
这篇关于在jpa hibernate SQL Server中将float / int转换为日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文