Implementation of java serialization and deserialization object to mysql

Source: Internet
Author: User

How to save a complex java object in a MySQL table

If you want to save complex java objects to MySQL you can serialize and save them as BLOB in a MySQL table.

For example you have an object "complexObject" from class "ComplexObject" and you want to save it in database.

The ComplexObject class must implements Serializable interface and you can serialize the objects like this:

ByteArrayOutputStream baos;

ObjectOutputStream out;

Baos = new ByteArrayOutputStream ();

Try {

Out = new ObjectOutputStream (baos );

Out. writeObject (complexObject );

Out. close ();

} Catch (IOException e ){

E. printStackTrace ();

}

Byte [] byteObject = baos. toByteArray ();

To deserialize the object:

ByteArrayInputStream bais;

ObjectInputStream in;

Try {

Bais = new ByteArrayInputStream (byteObject );

In = new ObjectInputStream (bais );

ComplexObject = (ComplexObject) in. readObject ();

In. close ();

} Catch (IOException ex ){

Ex. printStackTrace ();

} Catch (ClassNotFoundException ex ){

Ex. printStackTrace ();

}

The MySQL table looks like this:


Create table myTable (

...

ComplexObject BLOB ,...


);

If you use Hibernate and Annotation you declare the complexObject transient, and a byte [] byteObject that will be persisted:

@ Entity

@ Table (name = "myTable ")

SomeClass {

Private byte [] byteObject;

Private ComplexObject complexObject;

...

@ Transient

Public ComplexObject getComplexObject (){

Return complexObject;

}

Public void setComplexObject (ComplexObject complexObject ){

This. complexObject = complexObject;

ByteArrayOutputStream baos;

ObjectOutputStream out;

Baos = new ByteArrayOutputStream ();

Try {

Out = new ObjectOutputStream (baos );

Out. writeObject (complexObject );

Out. close ();

} Catch (IOException e ){

E. printStackTrace ();

}

This. byteObject = baos. toByteArray ();

}

@ Column (columnDefinition = "blob ")

Public byte [] getByteObject (){

Return byteObject;

}

Public void setByteObject (byte [] byteObject ){

ByteArrayInputStream bais;

ObjectInputStream in;

Try {

Bais = new ByteArrayInputStream (byteObject );

In = new ObjectInputStream (bais );

ComplexObject = (ComplexObject) in. readObject ();

In. close ();

} Catch (IOException ex ){

Ex. printStackTrace ();

} Catch (ClassNotFoundException ex ){

Ex. printStackTrace ();

}

This. byteObject = byteObject;

}

}

Attention the table column must be BLOB, not varchar.

Because the ComplexObject is transient it will not be save in database, but the byteObject will be save.

 

 

Author: xi_hong_shi

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.