I need to map Oracle XMLType column to hibernate entity class. There is a working (and I think well-known) solution that involves implementing UserType; however
To simplify Celso's answer further, one can avoid creating a custom function by using Oracle's built-in function
XMLType.createxml(?)
that can handle NULLs.
So the following annotations combined with Celso's custom dialect class works well.
@Lob
@ColumnTransformer(read = "NVL2(EVENT_DETAILS, (EVENT_DETAILS).getClobVal(), NULL)", write = "XMLType.createxml(?)")
@Column(name = "EVENT_DETAILS")
private String details;
You might also have to register the clob as xmltype in your custom dialect. So effectively you will have the following:
public class OracleDialectExtension extends org.hibernate.dialect.Oracle10gDialect {
public OracleDialectExtension() {
super();
registerColumnType(Types.CLOB, "xmltype");
}
@Override
public boolean useInputStreamToInsertBlob() {
return false;
}
}
Ensure to set your custom dialect in your hibernate configuration's session-factory property list: