1
package org.hibernate.test.typeparameters;
3
import java.io.Serializable;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
8
import java.util.Properties;
10
import org.slf4j.LoggerFactory;
11
import org.hibernate.HibernateException;
12
import org.hibernate.usertype.ParameterizedType;
13
import org.hibernate.usertype.UserType;
19
public class DefaultValueIntegerType implements UserType, ParameterizedType, Serializable {
21
private Integer defaultValue;
23
public int[] sqlTypes() {
24
return new int[] {Types.INTEGER};
27
public Class returnedClass() {
31
public boolean equals(Object x, Object y) throws HibernateException {
32
if (x==y) return true;
33
if (x==null || y==null) return false;
37
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
38
Number result = (Number) rs.getObject(names[0]);
39
return result==null ? defaultValue : new Integer(result.intValue());
42
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
43
if (value == null || defaultValue.equals(value) ) {
44
LoggerFactory.getLogger( getClass() ).trace("binding null to parameter: " + index);
45
st.setNull(index, Types.INTEGER);
47
LoggerFactory.getLogger( getClass() ).trace("binding " + value + " to parameter: " + index);
48
st.setInt(index, ((Integer)value).intValue());
52
public Object deepCopy(Object value) throws HibernateException {
53
return new Integer(((Integer)value).intValue());
56
public boolean isMutable() {
60
public int hashCode(Object x) throws HibernateException {
64
public Object assemble(Serializable cached, Object owner)
65
throws HibernateException {
69
public Serializable disassemble(Object value) throws HibernateException {
70
return (Serializable) value;
73
public Object replace(Object original, Object target, Object owner)
74
throws HibernateException {
78
public void setParameterValues(Properties parameters) {
79
this.defaultValue = Integer.valueOf((String) parameters.get("default"));