[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://m.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2008-12-25 15:43
zhyiwww 閱讀(2896)
評論(0) 編輯 收藏 所屬分類:
j2ee 、
database