[英]Hibernate returns null for time '00:00:00'
我將 spring hibernate 用於 web 應用程序。 它有一個返回開放時間的OpenHours
模式。 它有一個字段sundayOpen
type="time"
。 每當數據庫列“ sundayOpen ”中的時間等於“00:00:00”時, sundayOpen
就會返回null
。 但所有其他時間它都會返回正確的時間。 我在 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>
營業時間.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.