休眠 returns 时间 '00:00:00' 为空

Hibernate returns null for time '00:00:00'

我正在为 Web 应用程序使用 spring 休眠。它有一个 OpenHours 模式,return 开放时间。它有一个字段 sundayOpen type="time"。只要数据库列“sundayOpen”中的时间等于“00:00:00”,sundayOpen 就是 returning null。但所有其他时间 return 都是正确的时间。 我在 hibernate 5.4.2 和 3.6.0 中遇到过这个问题。

有什么方法可以获取“00:00:00”而不是 null

OpenHours.hbm.xml

<class name="com.example.OpenHours" table="openHours" schema="abxd_db">
        <id name="id" type="long">
            <column name="id"/>
            <generator class="assigned"/>
        </id> 
        <property name="sundayOpen" type="time">
            <column length="16" name="sundayOpen"/>
        </property>
</class>

OpenHours.java

 public class OpenHours implements Serializable {

    private long id;
     private Time sundayOpen; 

    @Id
    @Column(name = "id", nullable = false)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Time getSundayOpen() {
        return sundayOpen;
    }

    public void setSundayOpen(Time sundayOpen) {
        this.sundayOpen = sundayOpen;
    } 
}

您可以创建自定义 UserType

public class CustomTimeUserType implements UserType {
    public void nullSafeSet(PreparedStatement ps, Object value, int index) throws HibernateException, SQLException {
        if (value == null)
            ps.setString(index, "00:00:00");
        else
            ps.setDate(index, (Time) value);
    }
   // implement other methods

}

并在属性、

中使用
<property name="sundayOpen" type="time">
    <column length="16" name="com.pkg.CustomTimeUserType"/>
</property>

我不确定,但您也可以尝试使用 zeroDateTimeBehavior

hibernate.connection.zeroDateTimeBehavior=round