Oracle DB:映射日期字段类型,具有从db到实体模型字段的特定格式

时间:2022-09-11 16:27:13

I am trying to map a field with type Date into my entity from Oracle DB which looks like this: 10.06.1999 10:58:45

我试图将类型为Date的字段从Oracle DB映射到我的实体,如下所示:10.06.1999 10:58:45

I've tried with different types, java.util.Date, java.sql.Date - they all throw exceptions while trying to read the data form db into the model.

我尝试过不同类型的java.util.Date,java.sql.Date - 它们在尝试将数据表单db读入模型时都会抛出异常。

Only java.sql.Time doesn't throw exceptions. It isn't suitable for my case, because the UI then only displays the time and when saving this value from UI overwrites the data in the database with time only and not date e.g: 10:58:45.

只有java.sql.Time不会抛出异常。它不适合我的情况,因为UI只显示时间,当从UI保存这个值时,只用时间覆盖数据库中的数据而不是日期,例如:10:58:45。

Is it a good way to use the (new Java 8) DateTime class in the entity mode class with a formatter inside the model to tell the model to parse the date correctly?

在实体模式类中使用(new Java 8)DateTime类并在模型中使用格式化器来告诉模型正确解析日期是一种好方法吗?

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

To map your database DATE column to java.util.Date (or other based classes) you need to add the @Temporal annotation.

要将数据库DATE列映射到java.util.Date(或其他基类),需要添加@Temporal注释。

http://docs.oracle.com/javaee/6/api/javax/persistence/Temporal.html

#1


1  

To map your database DATE column to java.util.Date (or other based classes) you need to add the @Temporal annotation.

要将数据库DATE列映射到java.util.Date(或其他基类),需要添加@Temporal注释。

http://docs.oracle.com/javaee/6/api/javax/persistence/Temporal.html