To upgrade to Hibernate 4, I need a single-column basic StructType. In Hibernate 4, org.hibernate.type.BasicTypeRegistry contains single-column types such as arrays, blobs and clobs, but no type for java.sql.Types.STRUCT (id = 2002).
I have a pre-existing database field/column defined as an oracle.sql.STRUCT named DB_OBJECT_TYPE. In Hibernate 3, I was able to implement this as a custom UserType with the code at the bottom of this post.
With Hibernate 4, this code results in a class cast exception because the connection is proxied, even if
* I use "session.connection()" instead of "statement.getConnection()" or
* I use the session factory to get the session and use the connection inside "session.doWork( ... )"
So I tried to use the Hibernate 4 way, "StructType.INSTANCE.set( statement, values, index, session );" but
* There is no such thing as a StructType
* SerializableType maps to VARBINARY in Oracle
* Breaking up the java.sql.Types.STRUCT into its basic types will make it cover more than one field/column
Hibernate 3 code:
Code:
public int[] sqlTypes() {
return new int[] {java.sql.Types.STRUCT};
}
public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, java.sql.Types.STRUCT, DB_OBJECT_TYPE);
} else {
final MyDBObjectClass myDBObject = (MyDBObjectClass) value;
final Object[] values = new Object[] { myDBObject.x, myDBObject.y, ... };
final Connection connection = statement.getConnection();
final oracle.sql.STRUCT myStruct = new oracle.sql.STRUCT(
oracle.sql.StructDescriptor.createDescriptor(DB_OBJECT_TYPE, connection),
connection, values);
statement.setObject(index, myStruct, java.sql.Types.STRUCT);
}
}