Processing of hibernate Oracle BLOB Data Types

Source: Internet
Author: User

Hibernate stores and reads blob objects in a special way and cannot operate as it normally processes data. The following is an example of processing the Oracle blob type in hibernate:

Oracle Database Table creation statement

Create Table Stu
(
ID number (2 ),
Name varchar2 (16 ),
Filename varchar2 (64 ),
Filedata blob,
Primary Key (ID)
);

Stu. Java File

Public class Stu implements java. Io. serializable {

// Fields

Private byte ID;
Private string name;
Private string filename;
Private byte [] filedata; // This process sets its attribute type to byte []

// Constructors

/** Default constructor */
Public Stu (){
}

The rest is omitted .....

The Stu. HBM. xml file is as follows:

<Hibernate-mapping>
<Class name = "com. aoyou. Mapping. Stu" table = "Stu" schema = "lixin03080">
<ID name = "ID" type = "Java. Lang. Byte">
<Column name = "ID" precision = "2" scale = "0"/>
<Generator class = "assigned"/>
</ID>
<Property name = "name" type = "Java. Lang. String">
<Column name = "name" length = "16"/>
</Property>
<Property name = "FILENAME" type = "Java. Lang. String">
<Column name = "FILENAME" length = "64"/>
</Property>

<Hibernate-mapping>
<Class name = "com. aoyou. Mapping. Stu" table = "Stu" schema = "lixin03080">
<ID name = "ID" type = "Java. Lang. Byte">
<Column name = "ID" precision = "2" scale = "0"/>
<Generator class = "assigned"/>
</ID>
<Property name = "name" type = "Java. Lang. String">
<Column name = "name" length = "16"/>
</Property>
<Property name = "FILENAME" type = "Java. Lang. String">
<Column name = "FILENAME" length = "64"/>
</Property>
<! -- Note that the type of filedata below is binary -->
<Property name = "filedata" type = "binary">
<Column name = "filedata"/>
</Property>
</Class>
</Hibernate-mapping>
<Property name = "filedata" type = "binary">
<Column name = "filedata"/>
</Property>
</Class>
</Hibernate-mapping>

Test in the main method:

Save data to the database:

Public static void main (string [] ARGs) throws ioexception {
Session session = new configuration (). Configure (). buildsessionfactory (). opensession ();
Transaction T = session. begintransaction ();

File file = new file ("d :\\ shopping website \ Phase II \ data warehouse design .txt ");
Bufferedreader BR = new bufferedreader (New filereader (File ));


Stu = new Stu ();
Stu. setid (2 );
Stu. setname ("Mary ");
Stu. setfilename ("Data List ");

String content = "";
String temp = "";
While (temp = Br. Readline ())! = NULL ){
Content + = temp;
}
BR. Close ();
Stu. setfiledata (content. getbytes ());
Session. Save (Stu );

T. Commit ();
}

Read database data:

Public static void main (string [] ARGs) throws ioexception {
Session session = new configuration (). Configure (). buildsessionfactory (). opensession ();
Transaction T = session. begintransaction ();

String hql = "from Stu ";
Query query = session. createquery (hql );
List list = query. List ();
If (list! = NULL ){
Stu = (Stu) list. Get (0 );
System. Out. println (STU. GETID ());
System. Out. println (STU. getname ());
System. Out. println (STU. getfilename ());
Byte [] B = Stu. getfiledata ();
System. Out. Print (new string (B, 0, B. Length ));
}

T. Commit ();
}

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.