Database varchar2 to clob combined with hibernate operations

Source: Internet
Author: User

One part of the most recent project is news management. The type of news content in the database is varchar2, and the maximum size of news content is 4000 bytes. You need to enter more bytes, so we have to increase it by changing its type from varchar2 to clob, so that our code also needs to be modified.

First, add the following code to the news. Java file:

Private clob newcontentclob; // 2010-08-25
Public clob getnewcontentclob (){
Return newcontentclob;
}

Public void setnewcontentclob (clob newcontentclob ){
This. newcontentclob = newcontentclob;
}

Second, modify the configuration file news. HBM. xml --- this step corresponds to the ing relationship.

<Property name = "newcontentclob">
<Column name = "new_content" SQL-type = "clob"/>
</Property>

Third: modify the original new news, modify the news method to convert string into clob and save it to the database

Public void save (News news)

{
Try {
Session session = getsession ();
// Initialize the summaryclob Field

Oracle. SQL. clob = oracle. SQL. clob. empty_lob ();

News. setnewcontentclob (clob );
Session. Save (News );
Session. Flush ();
// The lockmode. Upgrade mode must be used for lock refresh.
Session. Refresh (News, lockmode. Upgrade );

// Obtain the value of the summaryclobstring attribute of the meetinghead object
String content = News. getnewcontent ();

// Assign the value of the obtained secondary field to the putstring () method of Oracle. SQL. clob.
// Value to the summaryclob field in the object

Org. hibernate. lob. serializableclob sclob = (Org. hibernate. lob. serializableclob) News
. Getnewcontentclob ();
Clob = (Oracle. SQL. clob) sclob. getwrappedclob ();
Clob. putstring (1, content );
} Catch (sqlexception e ){
E. printstacktrace ();
}
// Gethibernatetemplate (). Save (News); the original code is this line.
}

Fourth: the red part is the newly added code. The purpose is to convert the content (type: clob) of the database into a string and display it on the page.

Public News findnewsbyid (integer ID) throws aegeanexception
{
Try
{
News news = newsdao. Get (ID );
If (news! = NULL)
{
Java. SQL. clob = News. getnewcontentclob ();
If (clob! = NULL)
{
News. setnewcontent (clob. getsubstring (1, (INT) clob. Length ()));
}
Return news;
}
Return news;
}
Catch (exception E)
{
Log. debug (E. getmessage ());
Throw new aegeanexception ("error ");
}
}

 

Public list <News> findnewsbypage (PAGER pager) throws aegeanexception {
Try {
List <News> List = newsdao. findbypage (PAGER );
If (list! = NULL &&! List. isempty ())
{
List <News> newlist = new arraylist <News> ();
Iterator <News> iterator = List. iterator ();
While (iterator. hasnext ())
{
News news = iterator. Next ();
Java. SQL. clob = News. getnewcontentclob ();
If (clob! = NULL)
{
News. setnewcontent (clob. getsubstring (1, (INT) clob. Length ()));
}
Newlist. Add (News );
}
Return list;
}
Return list;
} Catch (exception e ){
Log. debug (E. getmessage ());
Throw new aegeanexception ("error ");
}
}

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.