我正在尝试使用定义将数据添加到 oracle 11g 数据库中的表中。
CREATE TABLE FOO_TIME_TEST
(ID NUMBER(20) NOT NULL, TIME TIMESTAMP(6),
CONSTRAINT FOO_TIME_TEST_PK PRIMARY KEY (ID) USING INDEX);
CREATE UNIQUE INDEX "foo_time_test_idx" ON "FOO_TIME_TEST" (TIME)
使用 Joda 时间的 Hibernate/JPA 映射如下
@Type(type = "com.foo.PersistentDateTime")
@Column(name = "TIME")
public DateTime getTime() {
return time;
}
映射类 PersistentDateTime 相关位
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws SQLException {
if (value == null) {
Hibernate.TIMESTAMP.nullSafeSet(preparedStatement, null, index);
} else {
Hibernate.TIMESTAMP.nullSafeSet(preparedStatement, ((DateTime) value).toDate(), index);
}
}
public int[] sqlTypes() {
return new int[] { Types.TIMESTAMP, };
}
编写的代码是这样的,它在使用 plusTime() 的循环中,但我将它隔离到这两个调用中。
{
DateTime time = new DateTime(2012, 10, 28, 0, 0, DateTimeZone.UTC);
FooTest data = new FooTest();
data.setTime(time);
em.persist(data);
System.out.println(time + " " + time.getMillis() / 1000 / 60 / 60);
}
{
DateTime time = new DateTime(2012, 10, 28, 1, 0, DateTimeZone.UTC);
FooTest data = new FooTest();
data.setTime(time);
em.persist(data);
System.out.println(time + " " + time.getMillis() / 1000 / 60 / 60);
}
输出
2012-10-28T00:00:00.000Z 375384
2012-10-28T01:00:00.000Z 375385
19:23:42,081 WARN JDBCExceptionReporter:100 - SQL Error: 1, SQLState: 23000
19:23:42,081 ERROR JDBCExceptionReporter:101 - ORA-00001: unique constraint (Foo.foo_time_test_idx) violated
我尝试删除唯一索引,然后代码成功但数据库中的两行是相同的:
ID Time
1 28-OCT-12 01.00.00.000000000 AM
2 28-OCT-12 01.00.00.000000000 AM
启用休眠跟踪后,似乎显示两个不同的日期被展平为相同的值。
Hibernate: insert into FOO_TIME_TEST (ID, TIME) values (?, ?)
07:38:54,217 TRACE TimestampType:151 - binding '2012-10-28 01:00:00' to parameter: 22
Hibernate: insert into FOO_TIME_TEST (ID, TIME) values (?, ?)
07:38:54,217 TRACE TimestampType:151 - binding '2012-10-28 01:00:00' to parameter: 22
确实需要 ojdbc-6.jar 的源代码来进一步调试...
我通过下载 11.2.0.2.0 的 ojdbc6_g.jar 并将类路径以及 -Doracle.jdbc.Trace=true 选项(如此处建议)和自定义 java util log.properties 启用了更多调试
这会在每次绑定调用后提供附加信息,在这两种情况下都会显示 01:00 值。
Nov 2, 2012 8:20:21 AM oracle.jdbc.driver.OraclePreparedStatementWrapper setTimestamp
TRACE_30: 3C322E7D Enter: 22, 2012-10-28 01:00:00.0
Nov 2, 2012 8:20:21 AM oracle.jdbc.driver.OraclePreparedStatement setTimestamp
FINE: 3C322E7D Public Enter: 22, 2012-10-28 01:00:00.0
Nov 2, 2012 8:20:21 AM oracle.jdbc.driver.OraclePreparedStatement setTimestampInternal
FINER: 3C322E7D Enter: 22, 2012-10-28 01:00:00.0