简体   繁体   English

JPA将简单的String对象映射到Oracle Date类型?

[英]JPA map simple String object to Oracle Date type?

I have a table in which date column is stores as varchar. 我有一个表,其中日期列存储为varchar。 while doing the Object relational mapping (i am using eclipselink) i want to map this string/varchar to Date object in java. 在执行对象关系映射时(我正在使用eclipselink),我想将此字符串/ varchar映射到Java中的Date对象。

My table is : 我的桌子是:

T_BUSINESS
  TRAN_ID        Number, 
  BUSINESS_DATE  Varchar

and I want to select all the rows where business date is between two input dates, here problem is since BUSINESS_DATE is varchar i cannot carry out the comparison. 并且我想选择营业日期在两个输入日期之间的所有行,这里的问题是因为BUSINESS_DATE是varchar我无法进行比较。 I can't modify the table and i don't wanna use the Native sql. 我无法修改表,也不想使用本机sql。

Can anybody tell me how can I do this. 谁能告诉我该怎么做。

Thanks in advance! 提前致谢!

You can use a TypeConverter to map this, 您可以使用TypeConverter对此进行映射,

See, 看到,

http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_typeconverter.htm#CHDHCCJF http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_typeconverter.htm#CHDHCCJF

Doing comparisons in queries may be more difficult, I think the default date format is yyyy-mm-dd so a varchar comparison may work. 在查询中进行比较可能会更困难,我认为默认日期格式为yyyy-mm-dd,因此可以进行varchar比较。 Otherwise you really need a real DATE field on the database. 否则,您确实需要数据库上的真实DATE字段。 You could use a database function to convert the varchar to a DATE for comparison, such as using CAST or FUNCTION which EclipseLink support is JPQL. 您可以使用数据库函数将varchar转换为DATE以进行比较,例如使用CAST或FUNCTION,而EclipseLink支持的是JPQL。

See, http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_cast.htm#cast 参见http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_cast.htm#cast

http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_func.htm#func http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_func.htm#func

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM