我正在为一个web应用程序使用spring hibernate。它有一个 OpenHours
模式,返回开放时间。它有一个字段 sundayOpen
type="time"
. 每当数据库列''中的时间周日开放'等于'00:00:00'。sundayOpen
正在返回 null
. 但其他时候它都会返回正确的时间。我在hibernate 5.4.2和3.6.0中也遇到过这个问题。
有什么办法可以让'00:00: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