简体   繁体   English

Hibernate java.sql.Date字段仅返回Oracle的日期

[英]Hibernate java.sql.Date field return only date for Oracle

I am using oracle database for retrieving date and time but it is returning only date when I am using java.sql.Date . 我正在使用oracle数据库检索日期和时间,但是当我使用java.sql.Date时,它仅返回日期。 I have tried with java.sql.Timestamp but its gave exception as wrong column type. 我曾尝试使用java.sql.Timestamp,但由于错误的列类型而给了例外。 I tried with V8Compatible configuration. 我尝试使用V8Compatible配置。 But it is also not working. 但这也不起作用。 I cannot change anything in the DB side. 我无法在数据库端进行任何更改。 In DB that column is of type DATE. 在数据库中,该列的类型为DATE。

Please Give a suggestion 请给个建议

For this, you should use ojdbc14.jar and oracle.sql.DATE 为此,您应该使用ojdbc14.jar和oracle.sql.DATE

Source 资源

You can try for inserting timestamp with precision of 2. Check this Stack Overflow Question . 您可以尝试插入精度为2的时间戳。检查此Stack Overflow Question As Oracle Date datatype is equivalent to java.sql.Timestamp in Java. 由于Oracle Date数据类型等效于Java中的java.sql.Timestamp。 Reference 参考

Or simply go for java.util.Date, It should work. 或只是去java.util.Date,它应该工作。 An easy way to format and insert. 格式化和插入的简便方法。 Something like this example 这个例子

某些代码会很好,但是您是否尝试使用@Temporal(TemporalType.TIMESTAMP)注释日期字段?

Prior to 9.2, the Oracle JDBC drivers mapped the DATE SQL type to java.sql.Timestamp. 9.2之前的版本,Oracle JDBC驱动程序将DATE SQL类型映射到java.sql.Timestamp。 This made a certain amount of sense because the Oracle DATE SQL type contains both date and time information as does java.sql.Timestamp. 这在一定程度上是有道理的,因为Oracle DATE SQL类型像java.sql.Timestamp一样包含日期和时间信息。 The more obvious mapping to java.sql.Date was somewhat problematic as java.sql.Date does not include time information. 到java.sql.Date的更明显的映射有些问题,因为java.sql.Date不包含时间信息。 It was also the case that the RDBMS did not support the TIMESTAMP SQL type, so there was no problem with mapping DATE to Timestamp. 在这种情况下,RDBMS不支持TIMESTAMP SQL类型,因此将DATE映射到Timestamp也没有问题。

In 9.2 TIMESTAMP support was added to the RDBMS. 在9.2中,TIMESTAMP支持已添加到RDBMS。 The difference between DATE and TIMESTAMP is that TIMESTAMP includes nanoseconds and DATE does not. DATE和TIMESTAMP之间的区别在于TIMESTAMP包括纳秒,而DATE不包括。 So, beginning in 9.2, DATE is mapped to Date and TIMESTAMP is mapped to Timestamp. 因此,从9.2开始,DATE映射到Date,而TIMESTAMP映射到Timestamp。 Unfortunately if you were relying on DATE values to contain time information, there is a problem. 不幸的是,如果您依赖于DATE值来包含时间信息,则会出现问题。

There are several ways to address this problem in the 9.2 through 10.2 drivers: 在9.2至10.2驱动程序中,有几种方法可以解决此问题:

Alter your tables to use TIMESTAMP instead of DATE. 更改表以使用TIMESTAMP而不是DATE。 This is probably rarely possible, but it is the best solution when it is. 这可能极少发生,但在可能的情况下是最佳解决方案。 For more information 欲获得更多信息
Refer: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_01 请参阅: http : //www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_01

make sure go with below code once, as first one is for timestamp creation and then go with get method for retrieving the date android time from oracle database. 确保一次使用以下代码,因为第一个用于创建时间戳,然后使用get方法从oracle数据库中检索日期android时间。

...................... ......................

java.util.Date android = new java.util.Date();
      long t = android.getTime();
      java.sql.Date sqlDate = new java.sql.Date(t);
      java.sql.Time sqlTime = new java.sql.Time(t);
      java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);

...................... ......................

pstmt.getDate(2, sqlDate);
      pstmt.getTime(3, sqlTime);
      pstmt.getTimestamp(4, sqlTimestamp);
      pstmt.executeUpdate();

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

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