如何使hibernate在查询后不更新XMLType列

时间:2013-05-16 07:52:24

标签: java hibernate auto-update xmltype

当我使用hibernate查询ORM映射对象时,它总是立即将对象更新到数据库。我已经检查了这个问题,发现它是由映射属性“appliedTestConfig”引起的。当我从映射文件中删除此属性时,查询操作只是查询操作。

有人可以给我一些帮助吗?

以下是映射文件的一部分。

<hibernate-mapping default-lazy="false">
<class name="cs.bean.CSBucket" table="CS_BUCKET" schema="DB2ADMIN">
    <id name="bucketId" type="java.lang.Long">
        <column name="BUCKET_ID" />
        <generator class="native" />
    </id>
    <property name="bucketName" type="java.lang.String">
        <column name="BUCKET_NAME"  />
    </property>
    <property name="appliedTestConfig" type="cs.bean.AppliedTestConfig">
        <column name="APPLIED_TEST_CONFIG" />
    </property>
</class>
</hibernate-mapping>

以下是CSBucket.java的一部分。

public class CSBucket implements java.io.Serializable {
// Fields
private Long bucketId;
private String bucketName;
private AppliedTestConfig appliedTestConfig;


public CSBucket(){  
}

public Long getBucketId() {
    return bucketId;
}

public void setBucketId(Long bucketId) {
    this.bucketId = bucketId;
}

public String getBucketName() {
    return bucketName;
}

public void setBucketName(String bucketName) {
    this.bucketName = bucketName;
}

public AppliedTestConfig getAppliedTestConfig() {
    return appliedTestConfig;
}

public void setAppliedTestConfig(AppliedTestConfig appliedTestConfig) {
    this.appliedTestConfig = appliedTestConfig;
}

}

以下是AppliedTestConfig.java文件的一部分。它扩展了UserXMLType。

  public class AppliedTestConfig extends UserXMLType {

private static final long serialVersionUID = 1L;

private List<String> jccDrivers = new ArrayList<String>();
private List<DB2Server> db2Servers = new ArrayList<DB2Server>();

public Serializable disassemble(Object value) throws HibernateException {
    if ((value instanceof AppliedTestConfig) && value != null) {
        // System.out.println("into the applied");
        List<DB2Server> db2List = ((AppliedTestConfig) value)
                .getDb2Servers();
        List<String> jccList = ((AppliedTestConfig) value).getJccDrivers();

        StringBuffer strBuf = new StringBuffer();
        strBuf = strBuf.append("<applied_test_config>");
        if (null != db2List && db2List.size() > 0) {
            strBuf = strBuf.append("<db2server_cofig>");

            for (int i = 0; i < db2List.size(); i++) {
                strBuf = strBuf.append("<db2server>");
                // strBuf.append(list.get(i));
                DB2Server db2server = (DB2Server) db2List.get(i);
                strBuf.append("<os>");
                strBuf.append(db2server.getOs());
                strBuf.append("</os>");
                strBuf.append("<version>");
                strBuf.append(db2server.getVersion());
                strBuf.append("</version>");
                strBuf.append("<dbcs>");
                if ("".equals(db2server.getDbcs())
                        || null == db2server.getDbcs()) {
                    strBuf.append("NULL");
                } else {
                    strBuf.append(db2server.getDbcs());
                }
                strBuf.append("</dbcs>");
                strBuf = strBuf.append("</db2server>");
            }

            strBuf = strBuf.append("</db2server_cofig>");
        }
        if (null != jccList && jccList.size() > 0) {
            strBuf = strBuf.append("<jccdriver_config>");
            for (int i = 0; i < jccList.size(); i++) {
                strBuf = strBuf.append("<jccdriver>");
                strBuf = strBuf.append((String) jccList.get(i));
                strBuf = strBuf.append("</jccdriver>");
            }
            strBuf = strBuf.append("</jccdriver_config>");
        }

        strBuf = strBuf.append("</applied_test_config>");

        return strBuf.toString();
    } else
        return null;
}

public Object assemble(Serializable cached, Object owner)
        throws HibernateException {
    jccDrivers = new ArrayList<String>();
    db2Servers = new ArrayList<DB2Server>();
    if (cached != null) {
        String strVal = (String) cached;
        Document doc = XMLTool.loadDocumentFromStr(strVal);
        if (doc != null) {
            Node db2Node = XMLTool.getSingleNode(doc, "db2server_cofig");
            NodeList db2NodeList = null;
            if (db2Node != null) {
                db2NodeList = XMLTool.getMultiNodes(db2Node, "db2server");
            } else {
                // Do nothing
            }

            Node jccNode = XMLTool.getSingleNode(doc, "jccdriver_config");
            NodeList jccNodeList = null;
            if (jccNode != null) {
                jccNodeList = XMLTool.getMultiNodes(jccNode, "jccdriver");
            } else {
                // Do nothing
            }

            if (jccNodeList != null) {
                for (int i = 0, n = jccNodeList.getLength(); i < n; i++) {
                    if (jccNodeList.item(i) instanceof Node) {
                        Node subNode = jccNodeList.item(i);
                        String nodeValue = XMLTool.getNodeValue(subNode);
                        if (nodeValue != null
                                && !"".equals(nodeValue.trim())) {
                            jccDrivers.add(nodeValue);
                        }
                    }
                }
            }
            // set db2Server
            if (db2NodeList != null) {
                for (int i = 0, n = db2NodeList.getLength(); i < n; i++) {
                    if (db2NodeList.item(i) instanceof Node) {
                        Node osNode = XMLTool.getSingleNode(db2NodeList
                                .item(i), "os");
                        Node versionNode = XMLTool.getSingleNode(
                                db2NodeList.item(i), "version");
                        Node dbcsNode = XMLTool.getSingleNode(db2NodeList
                                .item(i), "dbcs");
                        String osValue = XMLTool.getNodeValue(osNode);
                        String versionValue = XMLTool
                                .getNodeValue(versionNode);
                        String dbcsValue = XMLTool.getNodeValue(dbcsNode);

                        DB2Server db2server = new DB2Server(osValue,
                                versionValue, dbcsValue);
                        db2Servers.add(db2server);
                    }
                }
            }
        }
    }
    AppliedTestConfig one = new AppliedTestConfig();
    one.setDb2Servers(db2Servers);
    one.setJccDrivers(jccDrivers);
    return one;
}

public boolean equals(Object x, Object y) throws HibernateException {
    if (x == y)
        return true;
    if (x != null && y != null) {
        // todo: to modify this to detail equal
        return x.equals(y);
    }

    return false;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((db2Servers == null) ? 0 : db2Servers.hashCode());
    result = prime * result
            + ((jccDrivers == null) ? 0 : jccDrivers.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    AppliedTestConfig other = (AppliedTestConfig) obj;
    if (db2Servers == null) {
        if (other.db2Servers != null)
            return false;
    } else if (!db2Servers.equals(other.db2Servers))
        return false;
    if (jccDrivers == null) {
        if (other.jccDrivers != null)
            return false;
    } else if (!jccDrivers.equals(other.jccDrivers))
        return false;
    return true;
}

/**
 * Generate Getters and Setters....
 * */
}

以下是UserXMLType文件

public abstract class UserXMLType implements UserType, Serializable {


private static final long serialVersionUID = 1L;

private static final int[] TYPES = new int[] { Types.VARCHAR };
public int[] sqlTypes() {
    return TYPES;
}

public Class returnedClass() {
    // TODO Auto-generated method stub
    System.out.println("===>>>return Class is : " + this.getClass());
    return this.getClass();
}

public abstract boolean equals(Object first, Object second) throws HibernateException ;

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
    System.out.println("===>>>nullSafeGet is executed.");
    String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
    if (value != null) {
        return assemble(value,owner);
    } else {
        return null;
    }
}

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
    System.out.println("===>>>nullSafeSet is executed.");
    if (value != null) {
        Serializable str =disassemble(value);
        Hibernate.STRING.nullSafeSet(st, str, index);
    } else {
        Hibernate.STRING.nullSafeSet(st, value, index);
    }


}


public Object deepCopy(Object value) throws HibernateException {
    System.out.println("===>>>deepCopy is executed.");
    Object result=null;
    // dingk *
    //if (value==null) return "";
    if (value==null) return null;
    if (!(value instanceof Serializable)) {
        throw new HibernateException("Clone锟侥讹拷锟斤拷锟斤拷锟绞碉拷锟絡ava.io.Serializable锟接口o拷");
    }

    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = null;
    ObjectInputStream oi = null;
    try {
        oo = new ObjectOutputStream(bo);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        oo.writeObject(value);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());

    try {
        oi = new ObjectInputStream(bi);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        result = oi.readObject();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

public boolean isMutable() {
    return false;
}

public int hashCode(Object arg0) throws HibernateException {
    // TODO Auto-generated method stub
    return arg0.hashCode();
}


public abstract Serializable disassemble(Object obj) throws HibernateException ;

public abstract Object assemble(Serializable value, Object owner) throws HibernateException;

public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
    if (arg0==null) return null;
    if(this.isMutable()){
        return deepCopy(arg0);
    }
    else{
        return arg0;
    }
}

}

以下是我查询CSBucket时的控制台输出。

[13-5-16 15:32:50:109 CST] 00000022 SystemOut     O Hibernate: 
select
    csbucket0_.BUCKET_ID as BUCKET1_57_,
    csbucket0_.BUCKET_NAME as BUCKET2_57_,
    csbucket0_.LOC_IN_CMVC as LOC3_57_,
    csbucket0_.OWNER as OWNER57_,
    csbucket0_.ORIGINATOR as ORIGINATOR57_,
    csbucket0_.IS_MANUAL as IS6_57_,
    csbucket0_.BUCKET_TYPE as BUCKET7_57_,
    csbucket0_.RUN_TYPE as RUN8_57_,
    csbucket0_.CUSTOMIZE as CUSTOMIZE57_,
    csbucket0_.ONLINE_CHECK as ONLINE10_57_,
    csbucket0_.LINE_ITEM_DCR as LINE11_57_,
    csbucket0_.STORE_UDFS_LOC as STORE12_57_,
    csbucket0_.BUCKET_CLASS as BUCKET13_57_,
    csbucket0_.DESCRIPTION as DESCRIP14_57_,
    csbucket0_.DB_LUW as DB15_57_,
    csbucket0_.DB_ZOS as DB16_57_,
    csbucket0_.JCLS_NEEDED as JCLS17_57_,
    csbucket0_.INPUT_FILE_LOC as INPUT18_57_,
    csbucket0_.UTLITITES as UTLITITES57_,
    csbucket0_.TEST_FOCUS as TEST20_57_,
    csbucket0_.TC_NO as TC21_57_,
    csbucket0_.MAIN_FUNCTION as MAIN22_57_,
    csbucket0_.MAX_TIME as MAX23_57_,
    csbucket0_.OTHER_INSTRUCT as OTHER24_57_,
    csbucket0_.ACTIVE_STATUS as ACTIVE25_57_,
    csbucket0_.CREATOR as CREATOR57_,
    csbucket0_.UPDATOR as UPDATOR57_,
    csbucket0_.ACTIVATOR as ACTIVATOR57_,
    csbucket0_.PRE_BUCKET_ID as PRE29_57_,
    csbucket0_.CERTAIN_JCC as CERTAIN30_57_,
    csbucket0_.APPLIED_RELEASE as APPLIED31_57_,
    csbucket0_.APPLIED_TEST_CONFIG as APPLIED32_57_,
    csbucket0_.APPLIED_JDK as APPLIED33_57_,
    csbucket0_.TXT_PARM as TXT34_57_,
    csbucket0_.BUCKET_RUN as BUCKET35_57_,
    csbucket0_.BUCKET_FILE as BUCKET36_57_ 
from
    DB2ADMIN.CS_BUCKET csbucket0_ 
where
    csbucket0_.BUCKET_NAME like '%xml_index_aps_qad_j01%'
[13-5-16 15:32:52:250 CST] 00000022 SystemOut     O ===>>>nullSafeGet is executed.
[13-5-16 15:32:52:640 CST] 00000022 SystemOut     O ===>>>deepCopy is executed.
[13-5-16 15:32:52:828 CST] 00000022 SystemOut     O Hibernate: 
update
    DB2ADMIN.CS_BUCKET 
set
    BUCKET_NAME=?,
    LOC_IN_CMVC=?,
    OWNER=?,
    ORIGINATOR=?,
    IS_MANUAL=?,
    BUCKET_TYPE=?,
    RUN_TYPE=?,
    CUSTOMIZE=?,
    ONLINE_CHECK=?,
    LINE_ITEM_DCR=?,
    STORE_UDFS_LOC=?,
    BUCKET_CLASS=?,
    DESCRIPTION=?,
    DB_LUW=?,
    DB_ZOS=?,
    JCLS_NEEDED=?,
    INPUT_FILE_LOC=?,
    UTLITITES=?,
    TEST_FOCUS=?,
    TC_NO=?,
    MAIN_FUNCTION=?,
    MAX_TIME=?,
    OTHER_INSTRUCT=?,
    ACTIVE_STATUS=?,
    CREATOR=?,
    UPDATOR=?,
    ACTIVATOR=?,
    PRE_BUCKET_ID=?,
    CERTAIN_JCC=?,
    APPLIED_RELEASE=?,
    APPLIED_TEST_CONFIG=?,
    APPLIED_JDK=?,
    TXT_PARM=?,
    BUCKET_RUN=?,
    BUCKET_FILE=? 
where
    BUCKET_ID=?
[13-5-16 15:32:52:953 CST] 00000022 SystemOut     O ===>>>nullSafeSet is executed.
[13-5-16 15:32:53:171 CST] 00000022 SystemOut     O ===>>>deepCopy is executed.

现在,任何人都可以帮我进行查询操作而不更新数据库?非常感谢。

0 个答案:

没有答案