The most basic idea of operating blob with JDBC is to insert a record containing an empty blob, and then immediately open the record in the row lock mode to obtain reference to the Blob field, get an output stream from it, write the byte [] data, and then submit it. The basic idea of hibernate operations is the same.
Reference URL:
Http://biekwo.iteye.com/blog/323393
Http://bbs.csdn.net/topics/60009941
If you do not use Oracle to operate blob, an error that does not support new features will be reported.
This article of Robbin can also be used.
Http://www.iteye.com/topic/254
This is another idea, but I have not succeeded in the experiment.
Http://bbs.csdn.net/topics/220063661
If this error is reported
Operation not allowed: streams type cannot be used in batching
Refer to this URL
Http://hsyd.iteye.com/blog/320579
MyCode
1 Publicint add (User U ){ 2 3 // Todo auto-generated method stub 4 5 Try { 6 7 // Obtain the data ID 8 9 10 11 U. setid (getmax ()); 12 // Empty blob 13 U. setimage (hibernate. createblob (newbyte [1 ]); 14 Session session = Sessionfactory. opensession (); 15 16 Transaction = Session. begintransaction (); 17 Session. Save (U ); 18 19 Session. Flush (); 20 21 Session. Refresh (u, lockmode. Upgrade ); 22 23 // Get reference 24 25 Serializableblob sb =(Serializableblob) U. getimage (); 26 27 Blob blob = (Oracle. SQL. Blob) sb. getwrappedblob (); 28 29 Outputstream out = (Oracle. SQL. Blob) BLOB). getbinaryoutputstream (0 ); 30 31 32 33 34 35 File file = New File ("/users/Apple/downloads/project.png" ); 36 37 Fileinputstream FCM = New Fileinputstream (File ); 38 39 Byte [] Buff = Newbyte [FCM. Available ()]; 40 41 FS. Read (buff ); 42 43 FCM. Close (); 44 45 Out. Write (buff ); 46 47 Out. Close (); 48 49 50 51 // When done close the streams 52 53 54 55 // 56 57 Session. Flush (); 58 59 // 60 61 Transaction. Commit (); 62 63 System. Out. println ("Save success" ); 64 65 } Catch (Exception e ){ 66 67 System. Out. println (E. getmessage ()); 68 69 Return 0 ; 70 71 } 72 73 Return 1 ; 74 75 }