1 PackageCom.sun.demo;2 3 ImportJava.io.ByteArrayOutputStream;4 Importjava.io.IOException;5 ImportJava.io.ObjectInputStream;6 ImportJava.io.ObjectOutputStream;7 Importjava.io.Serializable;8 ImportJava.sql.Blob;9 Ten Importorg.hibernate.Hibernate; One A Public classSerializabledemoImplementsSerializable { - - Private Static Final LongSerialversionuid = 1L; the - PrivateBlob statistic; - - /** + * Convert object to BLOB type for saving to database - * + * @paramobj A * @return at */ - Publicjava.sql.Blob object2blob (Object obj) { - Try { -Bytearrayoutputstream out =NewBytearrayoutputstream (); -ObjectOutputStream OutputStream =NewObjectOutputStream (out); - outputstream.writeobject (obj); in byte[] bytes =Out.tobytearray (); - outputstream.close (); to returnHibernate.createblob (bytes); +}Catch(Exception e) { - e.printstacktrace (); the } * return NULL; $ }Panax Notoginseng - /** the * Convert BLOBs to Objects + * A * @paramDecblob the * @return + */ - PublicObject blob2object (Java.sql.Blob decblob) { $ Try { $ObjectInputStream in =NewObjectInputStream (Decblob.getbinarystream ()); -Object obj =In.readobject (); - in.close (); the returnobj; -}Catch(IOException e) {Wuyi e.printstacktrace (); the}Catch(Exception e) { - e.printstacktrace (); Wu } - return NULL; About } $ - PublicBlob getstatistic () { - returnstatistic; - } A + Public voidsetstatistic (Blob statistic) { the This. Statistic =statistic; - } $ the}
Here's a few notes.
One
In eclipse, there are two ways for us to quickly add serialversionuid.
Add default serial version ID:
Add generated serial version ID:
For the first, you need to know what is compatible and which are incompatible.
The second approach is to generate a hash value based on the structure of the class. Adding or subtracting a property, method, etc. can cause this value to change.
Serialization and deserialization of Java