Spring + hibernate processes Oracle clob (in editing)

Source: Internet
Author: User
In the past few days, due to project requirements, Oracle clob data is processed. Because Hibernate is used, there are too many articles on the Internet to address the hibernate1.x era, it is usually a forced conversion to Oracle. SQL. clob/blob, the result is certainly not good, because hibernate2, 3 encapsulates a layer, the returned is actually hibernate. serializableclob ..., I also read some usage of hibernate usertype, but there are always limitations, such as version requirements and server requirements...
The following operations are summarized and compared:
There are two types of drivers in a large range: OCI and thin. OCI supports transmission of> 4 K bytes, which is relatively convenient to process, however, you must install the Oracle client environment (configure the local net service name
URL: JDBC: oracle: oci8: @ Oracle)

If you want to transmit data larger than 4 K bytes, use the OCI driver. If you do not obtain Oracle clob implementation, you cannot transmit data larger than 4 K bytes;

Method 1.
Hibernate ing file:
  Name = "clob_content"
Type = "Java. SQL. clob"
Update = "true"
Insert = "true"
Column = "content"
Not-null = "false"/>
JavaBean: Private java. SQL. clob clob_content;

Daoimp:

Public void saveitem (item ){
 
String STR = item. getcontent ();

Item. setclob_content (hibernate. createclob (STR); // The clob is constructed and set to the object.
Gethibernatetemplate (). saveorupdate (item );
Gethibernatetemplate (). Flush ();
}

Public item getitem (long Itemid ){
Item = (item) gethibernatetemplate (). Get (item. Class, Itemid );
If (item! = NULL ){
Java. SQL. clob = item. getclob_content ();
Strint tempstr = clob. getsubstring (1, (INT) clob. Length ());
Item. setcontent (tempstr );
}
Return item;
}

Method 2: Use hibernate user type (reference: http://www.hibernate.org/76.html)
(1) hibernate ing file:
  Name = "clob_content"
Type = "stringclobtype"
Column = "content"
/>
(2) JavaBean: Private string clob_content;

(3)
/* Stringclobtype class source code */
Import java. Io. ioexception;
Import java. Io. reader;
Import java. Io. serializable;
Import java. Io. stringreader;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. types;
Import java. SQL. clob;Import org. hibernate. hibernate;
Import org. hibernate. hibernateexception;
Import org. hibernate. usertype. *; public class stringclobtype implements usertype {
Public int [] sqltypes (){
Return new int [] {types. clob };
} Public class returnedclass (){
Return string. Class;
} Public Boolean equals (Object X, object y ){
Return (x = y) | (X! = NULL & Y! = NULL & (X. Equals (y )));
} Public object nullsafeget (resultset RS, string [] names, object owner) throws hibernateexception, sqlexception {
Reader reader = Rs. getcharacterstream (Names [0]);
If (reader = NULL)
Return NULL; stringbuffer sb = new stringbuffer ();
Try {
Char [] charbuf = new char [4096];
For (INT I = reader. Read (charbuf); I> 0; I = reader. Read (charbuf )){
SB. append (charbuf, 0, I );
}
} Catch (ioexception e ){
Throw new sqlexception (E. getmessage ());
}
Return sb. tostring ();
} Public void nullsafeset (preparedstatement St, object value, int index) throws hibernateexception, sqlexception {
If (value! = NULL ){
Stringreader r = new stringreader (string) value );
St. setcharacterstream (index, R, (string) value). Length ());
} Else {
St. setnull (index, sqltypes () [0]);
}
} Public object deepcopy (object Value ){
If (value = NULL)
Return NULL;
Return new string (string) value );
} Public Boolean ismutable (){
Return false;
}/*
* @ See org. hibernate. usertype. usertype # hashcode (Java. Lang. Object)
*/
Public int hashcode (Object arg0) throws hibernateexception {
// Todo automatically generates method stubs
Return 0;
}/*
* @ See org. hibernate. usertype. usertype # disassemble (Java. Lang. Object)
*/
Public serializable disassemble (Object arg0) throws hibernateexception {
// Todo automatically generates method stubs
Return NULL;
}/*
* @ See org. hibernate. usertype. usertype # assemble (Java. Io. serializable, java. Lang. Object)
*/
Public object assemble (serializable arg0, object arg1) throws hibernateexception {
// Todo automatically generates method stubs
Return NULL;
}/*
* @ See org. hibernate. usertype. usertype # Replace (Java. Lang. Object, java. Lang. Object, java. Lang. Object)
*/
Public object Replace (Object arg0, object arg1, object arg2) throws hibernateexception {
// Todo automatically generates method stubs
Return NULL;
}
}

Daoimp:
GET/set is the same as other fields.

/* End: source code of the stringclobtype class */

Note: The above two methods cannot transmit more than 4 K Bytes when Oracle thin connection is used.
You can use the OCI connection method (JDBC: oracle: oci8: @ orclsid), but you must set the Propertie of hibernate.
0 (Note that this setting may cause the old code to be updated in batches. My solution is that other code uses a new data source)

/*
The Oracle10g JDBC driver can handle the clob type and simply use string, but with hiberbate, I don't know how to set its features:Setbigstringtryclob is true

Http://www.javaworld.com.tw/jute/post/view? Bid = 11 & id = 80295 & sty = 1 & TPG = 1 & age = 0
(The landlord said yes, but my test failed)

// Oracle official instructions and Examples
Http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/lob/LobToSP.html

Http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html

Http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/clob10g/handlingclobsinoraclejdbc10g.html
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.