2

I have deserialized a XML file with JAXB. Now I have classes with information thanks to the xml file. Now I need to put the data into mySql, do I need to serialize it again with JSON for example?

3 Answers3

0

If you are going to put an object into a database column, then you do need to serialize it.

HOWEVER - there are lots of reasons why you shouldn't put serialized objects into a database column. The most important one is that serialized data (JSON) in a tatabase table is not queryable ... except the the crudest way using %LIKE%.

A better idea is to map the fields of the Java classes to columns of tables. This can be done manually, or using an ORM technology such as Hibernate / JPA.

Another alternative is to use a so-called JSON database such as CouchDB or MongDB.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

The purpose of using mySQL or any other relational database is that you store your data as separate attributes. Using JSON in a relational database will enter all the data into one column instead of having separate columns for each piece of data.

If you really want to store your data in JSON format, you can try to use a NoSQL database like MongoDB. In MongoDB data is stored in JSON format.

Paco P
  • 9
  • 1
0

Well, MySQL can't store your Java object directly in the database. You need to convert it to a primitive data type that MySQL support.

Usually, objects stored in the database in their serialized form as BLOB. This approach has a couple of downsides though, probably the most important one is the limitation it imposes on which can read the data: you cannot read it from a different language without a lot of hustle. Also, the data is stored in unreadable form for human. On the other hand, this approach might save you from unmarshalling it again after reading it from the database next time.

You may also store it as string in a human-readable form like XML or JSON. I don't see any value from storing it as JSON instead of XML, except maybe that JSON could save you some extra bytes, but I don't think it worth it to go that way. With this approach, the data is stored in a human-readable form and can be easily read and unmarshalled using any language, unlike the serialized form. These advantages don't come for free though, you have a downside here as well: it's much heavier on size--but this shouldn't concern you at first unless you have a strict requirements on storage.

I don't recommend storing the object as a single value, you might want to store every field in a separate column. In that case, you need an ORM tool to help you with that.

mhadidg
  • 1,503
  • 17
  • 29