Wie man MySQL DATE '0000-00-00' & TIME '00: 00: 00 'mit Hibernate @ verbind
Zunächst müssen Sie "zeroDateTimeBehavior = convertToNull" am Ende der DB-Verbindungs-URL hinzufügen, damit beim Lesen von Datensätzen keine Daten in Nullen umgewandelt werden.
Zweitens müssen Sie Ihr Feld einem benutzerdefinierten UserType wie folgt zuordnen:
@Column(name = "start_date")
@Type(type = "com.my.dao.support.hibernate.MySqlDateUserType")
public Date getStartDate() {
return startDate;
}
@Column(name = "start_time")
@Type(type = "com.my.dao.support.hibernate.MySqlTimeUserType")
public Time getStartTime() {
return startTime;
}
Bitte beachten Sie, dass das Feld optional zugeordnet werden sollte.
Und schließlich suchen zwei Klassen, zuerst für DATE, nach TIME. Getestet mit Hibernate 3.6, MySQL 5.1, 5.5.
MySqlDateUserType
package com.th.dao.support.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.*;
/**
* Allows save MySql DATE '0000-00-00'
*/
public class MySqlDateUserType implements UserType {
private static final int[] SQL_TYPES = {Types.DATE};
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return Date.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
public int hashCode(Object arg0) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
// if the date is 0000-00-00 return null, else return the Date
Date result = null;
String strResult = resultSet.getString(names[0]);
if (strResult != null && !strResult.equals("0000-00-00"))
result = resultSet.getDate(names[0]);
return result;
}
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
// if the date is null set the value to "0000-00-00" else save the date
if (value == null)
statement.setString(index, "0000-00-00");
else
statement.setDate(index, (Date) value);
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
}
MySqlTimeUserType
package com.th.dao.support.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.*;
/**
* Allows save MySql TIME '00:00:00'
*/
public class MySqlTimeUserType implements UserType {
private static final int[] SQL_TYPES = {Types.TIME};
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return Time.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
public int hashCode(Object arg0) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
// if the time is 00:00:00 return null, else return the Time
Time result = null;
String strResult = resultSet.getString(names[0]);
if (strResult != null && !strResult.equals("00:00:00"))
result = resultSet.getTime(names[0]);
return result;
}
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
// if the time is null set the value to "00:00:00" else save the time
if (value == null)
statement.setString(index, "00:00:00");
else
statement.setTime(index, (Time) value);
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
}
Dieser Code basiert auf Prestons Klasse für TIMESTAMP:Hibernate benutzerdefinierte UserType funktioniert nicht