Galera, Bom dia !
Estou convertendo um tipo Numeric para um Date, da seguinte forma:
Minha classe de dominio Cliente
@Entity
@Table(schema = "CISPPESJ", name = "CLF00")
@IdClass(ClientePK.class)
public class Cliente implements Serializable {
private static final long serialVersionUID = 2078400015811447997L;
@Id
@Column(name = "TIPO")
private int tipoDocumento;
@Id
@Column(name = "NUMDOC")
private long identificacaoCliente;
@Column(name = "DTAFUN")
@Type(type = "br.cisp.tools.ConversorIntegerToDate")
@Temporal(TemporalType.DATE)
private Date dataFundacao;
Minha classe de conversão de tipo:
public class ConversorIntegerToDate implements UserType {
private static final int[] SQL_TYPES = { Types.NUMERIC };
private Logger log = Logger.getLogger(ConversorIntegerToDate.class);
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return Date.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
System.out.println("PASSANDO NO EQUALS");
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
log.debug("iniciando nullSafeGet com parametros: " +
"ResultSet ["+resultSet+"] " +
"String[] ["+names+"] " +
"Object ["+owner+"]");
int dateAsInteger = resultSet.getInt(names[0]);
if (resultSet.wasNull()) {
log.debug("retornando null pois resultSet == null");
return null;
}
if (dateAsInteger == Globals.ZERO) {
log.debug("retornando null pois data == zero");
return null;
}
String verificaData = String.valueOf(dateAsInteger);
if (verificaData.length() < 7) {
log.debug("retornando null pois data < 7 digitos: " + verificaData);
return null;
}
// Validação do mês
String mesStr = verificaData.substring(4, 6);
int mes = Integer.valueOf(mesStr);
if (mes == 0 || mes > 12) {
log.debug("retornando null pois mes invalido: " + mes);
return null;
}
// Validaçaõ do dia
String diaStr = verificaData.substring(4, 6);
int dia = Integer.valueOf(diaStr);
if (dia == 0 || dia > 31) {
log.debug("retornando null pois dia invalido: " + dia);
return null;
}
Date result = (Date) Tools.intToDate(dateAsInteger);
log.debug("converting user data type Integer to date : " + result);
return result;
}
public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
log.debug("iniciando nullSafeSet com parametros: " +
"PreparedStatement ["+statement+"] " +
"Object ["+value+"] " +
"index ["+index+"]");
if (value != null) {
Date date = (Date) value;
log.debug("data: " + date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
BigDecimal dateAsBigDecimal = BigDecimal.valueOf(Long.parseLong(sdf.format(date)));
statement.setBigDecimal(index, dateAsBigDecimal);
//Hibernate.INTEGER.nullSafeSet(statement, value, index);
} else {
// set zero as default
log.debug("null value");
statement.setBigDecimal(index, BigDecimal.ZERO);
//Hibernate.INTEGER.nullSafeSet(statement, Globals.ZERO, index);
}
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public Serializable disassemble(Object value) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public int hashCode(Object x) throws HibernateException {
// TODO Auto-generated method stub
return 0;
}
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
}
Acontece que quando busco no banco de dados eu consigo realizar a conversão certinho, ele lê o tipo numeric do banco e converte em Date. Mas o problema está ocorrendo quando realizo a operação inversa, na hora de gravar no banco de dados.
No método nullSafeSet(PreparedStatement statement, Object value, int index), sempre recebo o Object value como null.
Alguem já passou por isso ?
Abs