Hibernate终于搞定了CLOB字段得存取代码和大家分享

本文介绍了一种使用Hibernate ORM框架处理ORACLE9i数据库中CLOB类型数据的方法,并提供了具体的Java代码实现,包括插入、查询及更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

采用得是ORACLE9i数据库,Jboss或Weblogic。
JDBC采用ORACLE9i自带的Class12.jar
-------------
数据库结构:
java代码: 


CREATE TABLE SNCPARAMETERS
(
  ID     NUMBER ( 19 )                             NOT NULL,
  SNCID  NUMBER ( 19 ),
  NAME   VARCHAR2 ( 255 ),
  VALUE  CLOB
)


--------------
BO采用xdoclet建立的:
java代码: 


public class SNCParameters extends BaseObject
{

    /**
     * Returns the id.
     *
     * @return      long
     * @hibernate.id
     *          column = "id"
     *          type = "long"
     *          generator-class = "native"
     *          unsaved-value = "null"
     */

    public Long getId ( )
    {
        return id;
    }

    /**
     *    Sets the Id attribute of the SNCParameters object
     *
     * @param    id  The new Id value
     */

    public void setId ( Long id )
    {
        this. id = id;
    }


    /**
     * Returns the name.
     *
     * @return      String
     *
     * @hibernate.property
     *          column = "name"
     *          type = "string"
     *          not-null = "true"
     *          unique = "false"
     */


    public String getName ( )
    {
        return name;
    }

    /**
     *    Sets the Name attribute of the SNCParameters object
     *
     * @param    name  The new Name value
     */

    public void setName ( String name )
    {
        this. name = name;
    }

    /**
     * Returns the sncId.
     *
     * @return      Long
     *
     * @hibernate.property
     *          column = "sncId"
     *          type = "long"
     *          not-null = "true"
     *          unique = "false"
     */


    public Long getSncId ( )
    {
        return sncId;
    }

    /**
     *    Sets the SncId attribute of the SNCParameters object
     *
     * @param    sncId  The new SncId value
     */

    public void setSncId ( Long sncId )
    {
        this. sncId = sncId;
    }

    /**
     * Returns the values.
     *
     * @return      Clob
     *
     * @hibernate.property
     *          column = "value"
     *          type = "clob"
     *          not-null = "true"
     *          unique = "false"
     */


    public Clob getValue ( )
    {
        return value;
    }

    /**
     *    Sets the Values attribute of the SNCParameters object
     *
     * @param    values  The new Values value
     */

    public void setValue ( Clob value )
    {
        this. value = value;
    }
    private Long id;
    private Long sncId;
    private String name;
    private Clob value;
    private String valueString;
    public String getValueString ( )
    {
        return valueString;
    }
        public void setValueString ( String  valueString )
    {
        this. valueString = valueString;
    }
}



注:valueString并不映射到数据库的CLOB字段,只是方便需要使用这个BO的人用GET、SET 处理这个巨长的CLOB字段
------------
xdocLet生成的XML文件:
java代码: 


<?xml version=" 1. 0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "- //Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http: //hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    < class
        name="com. idncn. mc. bo. SNCParameters"
        table="SNCParameters"
        dynamic-update=" false"
        dynamic-insert=" false"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value=" null"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="string"
            update=" true"
            insert=" true"
            column="name"
            not- null=" true"
            unique=" false"
        />

        <property
            name="sncId"
            type="long"
            update=" true"
            insert=" true"
            column="sncId"
            not- null=" true"
            unique=" false"
        />

        <property
            name="value"
            type="clob"
            update=" true"
            insert=" true"
            column="value"
            not- null=" true"
            unique=" false"
        />
    </ class>

</hibernate-mapping>


--------------------
insert的代码:
java代码: 


    public List batchAddSncParameters ( List sncParametersList, Long sncId ) throws DbAccessException
    {
        logger. enterMethod ( );
        List ret = new ArrayList ( );
        try
        {
            sess = getSession ( );
            if (sncParametersList != null && sncParametersList. size ( ) > 0 )
            {
                for ( int i = 0; i < sncParametersList. size ( ); i++ )
                {
                    SNCParameters cp = (SNCParameters ) sncParametersList. get (i );
                    long newId = - 1;
                    if (cp != null )
                    {
                        SNCParameters cpNew = new SNCParameters ( );
                        cpNew. setSncId (sncId );
                        cpNew. setName (cp. getName ( ) );
                        cpNew. setValue (Hibernate. createClob (" " ) );
                        newId = ( ( Long ) sess. save (cpNew ) ). longValue ( );
                        sess. flush ( );

                        sess. refresh (cpNew, LockMode. UPGRADE );
                        String content = cp. getValueString ( );

                        String appserver = System. getProperty ("appserver", "jboss" );
                        if (!appserver. equalsIgnoreCase ("jboss" ) )
                        {
                            //weblogic
                            OracleThinClob clob = (OracleThinClob ) cpNew. getValue ( );
                            java. io. Writer pw = clob. getCharacterOutputStream ( );
                            pw. write (content );
                            pw. flush ( );
                            pw. close ( );
                        }
                        else
                        {
                            //jboss
                            oracle. sql. CLOB clob = (oracle. sql. CLOB ) cpNew. getValue ( );
                            java. io. Writer pw = clob. getCharacterOutputStream ( );
                            pw. write (content );
                            pw. flush ( );
                            pw. close ( );
                        }
                        ret. add ( new Long (newId ) );
                    }
                }
            }
        }
        catch ( Exception e )
        {
            logger. error (e );
            ErrorReason errorReason = new ErrorReason (ErrorReason. INSERT_OBJECT_FAILED_REASON );
            throw new DbAccessException (DbAccessException. DBA_OPERATE_EXCEPTION, errorReason );
        }
        finally
        {
            closeSession (sess );
            logger. exitMethod ( );
        }
        return ret;
    }


-----------------
注:Weblogic必须使用weblogic.jdbc.vendor.oracle.OracleThinClob
---------------------
读取CLOB字段:
java代码: 


    public List selectSncParametersBySncId (long sncId ) throws DbAccessException
    {
        logger. enterMethod ( );
        List ret = new ArrayList ( );
        try
        {
            sess = getSession ( );
            String query = "select cp from cp in class com. idncn. mc. bo. SNCParameters where cp. sncId = ?";
            logger. debug ("SQL=" + query );
            List iter = sess. find (query, new Long (sncId ), Hibernate. LONG );
            for ( int i = 0; i < iter. size ( ); i++ )
            {
                SNCParameters newCp = new SNCParameters ( );
                SNCParameters cp = (SNCParameters ) (iter. get (i ) );
                logger. debug ("after fetch:" + cp );
                newCp. setId (cp. getId ( ) );
                newCp. setSncId (cp. getSncId ( ) );
                newCp. setName (cp. getName ( ) );
                java. sql. Clob clob = cp. getValue ( );
                if (clob != null )
                {
                    logger. debug ("b===" + clob. length ( ) );
                    String b = clob. getSubString ( 1, ( int ) clob. length ( ) );
                    //logger.debug("b==="+b);
                    newCp. setValueString (b );
                }
                ret. add (newCp );
            }
        }
        catch ( Exception e )
        {
            logger. error (e );
            ErrorReason errorReason = new ErrorReason (ErrorReason. SELECT_FAILED_REASON );
            throw new DbAccessException (DbAccessException. DBA_OPERATE_EXCEPTION, errorReason );
        }
        finally
        {
            closeSession (sess );
            logger. exitMethod ( );
        }
        return ret;
    }


---------------
更新这个字段的代码:
java代码: 


    public void updateSncParameters (SNCParameters newParam ) throws DbAccessException
    {
        logger. enterMethod ( );
        try
        {
            sess = getSession ( );

            Long id = newParam. getId ( );
            SNCParameters pp = (SNCParameters ) sess. load (SNCParameters. class, id, net. sf. hibernate. LockMode. UPGRADE );

            pp. setSncId (newParam. getSncId ( ) );
            pp. setName (newParam. getName ( ) );
            pp. setId (newParam. getId ( ) );

            String newValue = newParam. getValueString ( );
            logger. debug ("Update Length =" + newValue. length ( ) );

            String appserver = System. getProperty ("appserver", "jboss" );
            logger. debug ("appserver: " + appserver );
            if (!appserver. equalsIgnoreCase ("jboss" ) )
            {
                //weblogic
                OracleThinClob clob = (OracleThinClob ) pp. getValue ( );
                if (pp != null )
                {
                    java. io. Writer pw = clob. getCharacterOutputStream ( );
                    pw. write (newValue );
                    pw. flush ( );
                    pw. close ( );
                }
            }
            else
            {
                //jboss
                oracle. sql. CLOB clob = (oracle. sql. CLOB ) pp. getValue ( );
                if (pp != null )
                {
                    java. io. Writer pw = clob. getCharacterOutputStream ( );
                    pw. write (newValue );
                    pw. flush ( );
                    pw. close ( );
                }
            }
        }
        catch ( Exception e )
        {
            logger. error (e );
            ErrorReason errorReason = new ErrorReason (ErrorReason. UPDATE_OBJECT_FAILED_REASON );
            throw new DbAccessException (DbAccessException. DBA_OPERATE_EXCEPTION, errorReason );
        }
        finally
        {
            closeSession (sess );
            logger. exitMethod ( );
        }
    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值