Hi there,
I would like to create custom type for hibernate to map to SqlServer 2005 Xml type column and then i mapped the custom class to one of my hbm property. Problem i got now is that i running hbm2ddl using hibernatetools from Ant and i got the error "No Dialect mapping for JDBC type: -1".
Currently iam using hibernate 3.2.5ga and SqlServer2005 jdbc with the dialect as org.hibernate.dialect.SQLServerDialect. for jdbc reference, here is the link
http://msdn2.microsoft.com/en-us/library/ms378813.aspx
Any helps would be really appreciated.
Cheers,
-T
Code:
<property name="xmlDocument" column="xml_column" type="SqlServerXmlType"/>
Code:
public class SqlServerXmlType implements UserType, Serializable {
private static final long serialVersionUID = -1626534232053785976L;
@SuppressWarnings("unchecked")
private static final Class RETURNED_CLASS = Document.class;
private static final int[] SQL_TYPES = new int[] {java.sql.Types.LONGVARCHAR};
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#returnedClass()
*/
@SuppressWarnings("unchecked")
public Class returnedClass() {
return RETURNED_CLASS;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return SQL_TYPES;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object aObject) throws HibernateException {
return aObject.hashCode();
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable aCached, Object aOwner)
throws HibernateException {
try {
return SqlServerXmlType.stringToDom((String)aCached);
}
catch (Exception e) {
throw new HibernateException("Could not assemble String to Document", e);
}
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object aObject) throws HibernateException {
try {
return SqlServerXmlType.domToString((Document)aObject);
}
catch (Exception e) {
throw new HibernateException("Could not disassemble Document to Serializable", e);
}
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object aOriginal, Object aTarget, Object aOwner) {
return deepCopy(aOriginal);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object aObject1, Object aObject2) throws HibernateException {
if(aObject1 == null && aObject2 == null) return true;
else if (aObject1 == null && aObject2 != null ) return false;
else return aObject1.equals(aObject2);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet aResultSet, String[] names, Object aOwner)
throws HibernateException, SQLException {
if(names.length != 1)
throw new HibernateException("names arrays has more than one element. can't handle this!");
String xmlStr= (String)aResultSet.getObject(names[0]);
try {
return (xmlStr != null)?SqlServerXmlType.stringToDom(xmlStr):null;
} catch (Exception e) {
throw new HibernateException(e);
}
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement aPreparedStm, Object aValue, int aIndex)
throws HibernateException, SQLException {
try {
String xmlStr = null;
if(aValue != null) {
xmlStr = SqlServerXmlType.domToString((Document) aValue);
}
aPreparedStm.setObject(aIndex, xmlStr);
} catch (Exception e) {
throw new SQLException("Could not convert document to String for storage");
}
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object aValue) throws HibernateException {
if(aValue == null) {
return null;
}
return ((Document)aValue).cloneNode(true);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
protected static String domToString(Document aDocument) throws TransformerException
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(aDocument);
StringWriter sw=new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
return sw.toString();
}
protected static Document stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(xmlSource.getBytes("UTF-8")));
}