I've been trying to get this work, but now I'm stuck. I apologize if I've missed something obvious. I have been reading the fine manual, searched the web and newsgroups without finding a solution.
I need to be able to work with an Oracle database that has a table that contains a column of type SYS.XMLType.
Let's say the table was created like this:
Code:
CREATE TABLE doc (
doc_id int not_null primary key,
doc_content SYS.XMLType
);
Then my mapping looks like this:
Code:
<class name="myorg.Doc" table="doc">
<id name="id" column="id" type="int">
<generator class="sequence">
<param name="sequence">SEQ_DOC</param>
</generator>
</id>
<property name="content" column="doc_content" type="Clob"/>
</class>
The problem is that the Oracle JDBC driver does not allow XMLType columns to be retrieved as Clobs from the ResultSet unless you use this syntax: (the column must be aliased for this to work)
Code:
SELECT d.doc_content.getClobVal() FROM doc d;
I have tried creating a UserType class that would convert the column from XMLType to char[], which works - almost. In order to do that, you first have to create an XMLType object from the Oracle OPAQUE object then get the CharacterStream from XMLType.
Here's the relevant snippet:
Code:
OPAQUE op=ors.getOPAQUE(names[0]);
XMLType xt=XMLType.createXML(op);
Reader rd=xt.getClobVal().getCharacterStream();
I then use rd to convert it into a char[].
The problem with that is that XMLType is internally converted to a DOM object that comes out nice and indented in the character stream. That causes problems for tags where whitespace is significant.
One solution to this would be to convert the table column to Clob instead of XMLType. Using a Clob column is the Oracle recommended way to retain whitespace, but that is currently impossible due to other requirements. For instance, XPath queries and Extract() only work on XMLType columns.
Long story short, my UserType will not work the way I implemented it.
I am at the point where I am ready to create my own Persister class to work around this problem. It would simply make the statement use .getClobVal() for any XMLType columns and read them as Clobs. XMLType columns accept Clobs for INSERTs, so there's no issues I'm aware of in that respect.
This does not strike me as a particularily elegant solution, so I'm looking for suggestions. Does anyone know a better way to do this? If implementing a persister is the best way, then what is the best approach to it?
If I can't get this to work, I might have to abandon Hibernate entirely in favor of a more manual approach.