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 ");
}
}