Hi All,
Environment: Hibernate 3.x, Weblogic 8.1, JDK 1.4.x
I have the following table:
Code:
create table XMLROW_COLLECTED (
XMLROWID NUMBER primary key,
XMLCONTENT XMLTYPE);
Since there is no direct support for Oracle XMLType I followed the example listed at
http://www.hibernate.org/405.htmlFollowing are the important code snippets of the custom UserType class:
Code:
public class HibernateXMLType implements UserType, Serializable {
// all other methods are ommitted at present for brevity
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
XMLType xmlType = null;
Document doc = null;
try {
OPAQUE value = null;
OracleResultSet ors = null;
if (rs instanceof OracleResultSet) {
ors = (OracleResultSet)rs;
} else {
throw new UnsupportedOperationException("ResultSet needs to be of type OracleResultSet");
}
value = ors.getOPAQUE(names[0]);
xmlType = XMLType.createXML(value);
Clob xmlClob = xmlType.getClobVal();
doc = XMLUtils.createDocumentObject(xmlClob.getCharacterStream());
}finally {
if (null != xmlType) {
xmlType.close();
}
}
return doc;
}
public void nullSafeSet(PreparedStatement stmt, Object value, int index)
throws HibernateException, SQLException {
XMLType xmlType = null;
try {
//If the value is null then set NULL and return
if (null == value) {
stmt.setNull(index, OracleTypes.OPAQUE, "SYS.XMLTYPE");
return;
}
if (stmt instanceof OraclePreparedStatement) {
xmlType = XMLType.createXML(stmt.getConnection(), XMLUtils.toXMLString((Document)value));
OraclePreparedStatement oracleStmt = (OraclePreparedStatement)stmt;
oracleStmt.setObject(index, xmlType);
}else {
throw new HibernateException("PreparedStatement object must be a OraclePreparedStatement");
}
}finally {
if (null != xmlType) {
xmlType.close();
}
}
}
Here is the code for the model object:
Code:
public class XMLRowCollected implements Serializable {
private Long xmlID;
private HibernateXMLType xmlData;
// Rest is getter/setter methods and default constructor
}
Here is the hbm.xml file:
Code:
<hibernate-mapping>
<class name="com.goofy.test.xml.XMLRowCollected" table="TESTINGXMLTYPE">
<id name="xmlID" unsaved-value="null" type="long">
<column name="XMLROWID" not-null="true"/>
<generator class="increment"/>
</id>
<property name="xmlData"
type="com.goofy.test.xml.HibernateXMLType"
column="XMLCONTENT"
update="true"
access="property"
insert="true"/>
</class>
</hibernate-mapping>
Before i move on - Is there anything wrong till now?
All i want to do is insert a row into XMLROW_COLLECTED table
I have some basic questions at this point:
1. Since the model class contains only 2 fields, and id and custom UserType, how will it hold the document object that will need to be available when nullSafeSet method gets called?
2. I guess the only way to insert a row will be to call Code:
session.save(xmlRowCollectedInstance);
. If that is the case then how will i populate/pass the document object of an XML that needs to be stored?
3. When does nullSafeSet be called? I assume that whenever session.save() is called, Am i right?
INSERT INTO ... VALUES... is not supported in Hibernate and if i directly user PreparedStatement then nullSafeSet method does not get called. So i am stuck.
I tried to google and all i got is "how to code a custom UserType" class for supporting XMLType and how to retrieve the content. But was not able to find any code of how to use UserType class created to insert a row containing an XML into XMLType type column in an Oracle table.
I am a newbie and would really appreciate any help/pointers.
Thanks,
Madhav[/i]