简体   繁体   English

Hibernate 将时间戳映射为可序列化

[英]Hibernate maps Timestamp as Serializable

I'm trying to setup hibernate in my new project and I have this problem.我正在尝试在我的新项目中设置休眠,但我遇到了这个问题。 I'm using oracle database.我正在使用oracle数据库。

In some tables I have more than one column that are timestamp.在某些表中,我有不止一列是时间戳。

Hibernate maps this columns as Serializable. Hibernate 将此列映射为 Serializable。

I tried to change manually to LocalTime type but the project won't even run.我尝试手动更改为 LocalTime 类型,但该项目甚至无法运行。 I change both on Availability.java and Availability.hbm.xml.我更改了 Availability.java 和 Availability.hbm.xml。

Is it supposed to be Serializable?它应该是可序列化的吗? I would like to use LocalTime instead.我想改用 LocalTime。 Is there a way to do this?有没有办法做到这一点?

I found this: How to map oracle timestamp to appropriate java type in hibernate?我发现了这个: How to map oracle timestamp to适当的 java type in hibernate? . . But it was 5 years ago and it seems like a complicated solution..但那是 5 年前的事了,这似乎是一个复杂的解决方案。

public class Availability  implements java.io.Serializable {
    private int id;
    private Teacher teacher;
    private byte month;
    private short year;
    private Serializable initialhour;
    private Serializable endhour;
    private String weekday;
    public void setInitialhour(Serializable initialhour) {
        this.initialhour = initialhour;
    }
    public Serializable getEndhour() {
        return this.endhour;
    }
}

You can just use:你可以只使用:

  private Timestamp initialhour;

If you want to can add annotation, but it should work just fine without it:如果您想添加注释,但没有它应该可以正常工作:

 @Temporal(TemporalType.TIMESTAMP)
 private Timestamp initialhour;

If you want to use java 8 DateTime you can use the @Type annotation, something like that:如果你想使用 java 8 DateTime 你可以使用 @Type 注释,就像这样:

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime initialhour;

Try to addhibernate-java8 to your project.尝试将hibernate-java8添加到您的项目中。 The issue is that LocalTime is a Java8 class which Hibernate does not support by default.问题是, LocalTime是一个Java8类Hibernate默认不支持。

同样的事情发生在我身上,也是“间隔到一天”数据类型,我发现手动解决这个问题的最简单方法是将“可序列化”替换为“时间戳”,不太干净但对我有用。

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

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