[1]表结构
CREATE TABLE "POI_BEIJING"."XML_TEST"
(
"ITEM_ID" NUMBER(10,0) NOT NULL ENABLE,
"ITEM_NAME" VARCHAR2(255 BYTE),
"ITEM_VALUE" "SYS"."XMLTYPE" ,
}
[2]自定义类型(参考网上资料)
参考了此老兄的思路http://blog.csdn.net/wmbb/archive/2006/08/10/1045742.aspxpackage com.csc.poimanager.dao.type;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.OPAQUE;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public class PoiAdditionalXmlType implements UserType, Serializable {
private static final Class returnedClass = String.class;
private static final int[] SQL_TYPES = new int[] { oracle.xdb.XMLType._SQL_TYPECODE };
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return returnedClass;
}
public boolean equals(Object arg0, Object arg1) throws HibernateException {
if (arg0 == null || arg1 == null) {
throw new HibernateException("None of the arguments can be null.");
}
if (arg0 instanceof oracle.xdb.XMLType
&& arg1 instanceof oracle.xdb.XMLType) {
return arg0.equals(arg1);
}
return false;
}
public int hashCode(Object arg0) throws HibernateException {
return 0;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)
throws HibernateException, SQLException {
OracleResultSet ors = (OracleResultSet) rs;
OPAQUE op = ors.getOPAQUE(names[0]);
oracle.xdb.XMLType xt = oracle.xdb.XMLType.createXML(op);
return xt.getStringVal();
} public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
Connection conn = st.getConnection();
OPAQUE aClob = oracle.xdb.XMLType.createXML(conn, (String) value);
st.setObject(index, aClob);
} public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object arg0) throws HibernateException {
return null;
}
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return null;
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return null;
}
}
[3]POJO
package com.csc.poimanager.dao;
import com.csc.poimanager.dao.type.PoiAdditionalXmlType;
public class XmlTest implements java.io.Serializable {
private Long itemId;
private Long poiId;
private String itemName;
private String itemValue; public XmlTest() {
}
public XmlTest(String itemName) {
this.itemName = itemName;
}
public Long getItemId() {
return this.itemId;
}
public void setItemId(Long itemId) {
this.itemId = itemId;
}
public Long getPoiId() {
return this.poiId;
}
public void setPoiId(Long poiId) {
this.poiId = poiId;
}
public String getItemName() {
return this.itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemValue() {
return this.itemValue;
}
public void setItemValue(String itemValue) {
this.itemValue = itemValue;
}
}
[4]映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.csc.poimanager.dao.XmlTest" table="XML_TEST">
<id name="itemId" type="java.lang.Long">
<column name="ITEM_ID" precision="10" scale="0" />
<generator class="increment" />
</id>
<property name="poiId" type="java.lang.Long">
<column name="POI_ID" precision="10" scale="0" />
</property>
<property name="itemName" type="java.lang.String">
<column name="ITEM_NAME" />
</property>
<property name="itemValue" type="com.csc.poimanager.dao.type.PoiAdditionalXmlType" >
<column name="ITEM_VALUE" />
</property> </class>
</hibernate-mapping>
[5]查询操作
XmlTestDAO xtdao = new XmlTestDAO();
Session sess = xtdao.getSession();
Transaction tx = sess.beginTransaction();
XmlTest xt =xtdao.findById((long)1);
// xtdao.save(xt);
System.out.println("xt item_value : " + xt.getItemName());
System.out.println("xt item_value : " + xt.getItemValue());
tx.commit();
sess.close();
System.out.println("getting xmltest ok ");
执行结果
xt item_value : WIFI
xt item_value : <?xml version="1.0"?>
<wifi>Yes</wifi>
getting xmltest ok
[6]插入操作
XmlTestDAO xtdao = new XmlTestDAO();
Session sess = xtdao.getSession();
Transaction tx = sess.beginTransaction();
XmlTest xt =xtdao.findById((long)1);
System.out.println("xt item_value : " + xt.getItemName());
System.out.println("xt item_value : " + xt.getItemValue());
tx.commit();
sess.close();
System.out.println("getting xmltest ok ");
执行结果:
saving xmltest ok
[8]注意
(1)oracle的XmlType不是字符串,是oracle的一种数据类型,就像varchar一样
(2)POJO中的itemValue类型是由你的自定义类解析后的对象类型
比如上面的实现,是把XmlType对象解析成xml的串,是字符串类型。所以在POJO中的定义是String类型,而不是PoiAdditionalXmlTyp类型
(3)此处仅实现了把oracle中XmlType的值解析成string串,同时,可以把xml的string串保存到XmlType类型的字段里面
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2008-12-25 15:43
zhyiwww 阅读(2882)
评论(0) 编辑 收藏 所属分类:
j2ee 、
database