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