This might not be quite the right place, as I am working in Grails/Groovy but hopefully the basic idea is still the same.
I have a class that looks like this (along with various constructors and a few other methods):
Code:
class SampleValue {
Float value
Boolean real
}
It is supposed to abstract away the fact that I have a whole heap of fields that need to be storing two values, but the user enters them in a single field. I then parse that string into the two values.
I am trying to create the class to map this to the database (if possible). What I have so far is the below. I have been reading
http://docs.jboss.org/hibernate/stable/ ... rType.html and I copied bits from
http://omaha-seattle.blogspot.com/2010/ ... -type.html but I really need an example that does something similar to what I want.
Code:
import org.hibernate.usertype.UserType
import java.sql.Types
import java.sql.PreparedStatement
import java.sql.ResultSet
class SampleValueUserType implements org.hibernate.usertype.UserType {
private static final SQL_TYPES = [Types.FLOAT, Types.BOOLEAN] as int[]
public Object assemble(Serializable cached, Object owner) {
cached
}
public Object deepCopy(Object o) {
o
}
public Serializable disassemble(Object value) {
value
}
public boolean equals(Object x, Object y) {
x.equals(y)
}
public int hashCode(Object o) {
o.hashCode()
}
public boolean isMutable() {
true
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) {
def value = rs.getFloat(names[0])
def real = rs.getBoolean(names[1])
if (value && real) {
return new SampleValue(value, real)
} else {
return null
}
}
public void nullSafeSet(PreparedStatement st, Object value, int index) {
// what is index??
//st.setBigDecimal(index, (java.math.BigDecimal)value)
}
public Object replace(Object original, Object target, Object owner) {
original
}
public Class returnedClass() {
SampleValue
}
public int[] sqlTypes() {
SQL_TYPES
}
}