I'm using spring hibernate for a web application. It has a OpenHours
modal which return the open time. It has a field sundayOpen
type="time"
. Whenever the time in database column 'sundayOpen' is equal to '00:00:00' the sundayOpen
is returning null
. But all the other times it returns the correct time.
I have experienced this issue with hibernate 5.4.2 and 3.6.0.
Is there any way to get '00:00:00' instead of 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;
}
}
You can create a custom 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
}
And use it in property,
<property name="sundayOpen" type="time">
<column length="16" name="com.pkg.CustomTimeUserType"/>
</property>
I am not sure but you can also try with zeroDateTimeBehavior
,
hibernate.connection.zeroDateTimeBehavior=round