简体   繁体   中英

Serialization vs DB Storage

I am currently studying Java Serialization. However, I am still confused about its practicality. Generally speaking, when are we supposed to use serialisation in comparison to storing data directly in various database columns?

I think you are confusing Serialization with reading and writing an Object to a database.

As explained in this SO answer Do Hibernate table classes need to be Serializable? JPA objects (which hibernate is) should implement Serializable so that detached entities can be sent to other layers of your application possibly via RMI.

This has nothing to do with how Hibernate reads and writes data to a database. As long as you don't use detached objects you can get away with not having your entities implement Serializable and hibernate will still work just fine.

Hibernate reads and writes to a database via JDBC just like you would if you were writing the SQL queries yourself. If you want to learn more about how hibernate converts your object fields to JDBC methods you can start by looking at Hibernate UserType . Hibernate comes by default with a lot of built in User Types that can convert ResultSet columns of types String , Date , int etc to and from the database. If you need to write your own UserTypes which happens on occasion, you just have to write your own UserType implementation which is pretty simple.

Hibernate-enhanced classes can actually be Serializable . However, you should think about all the outcomes before you use it that way. You may encounter those problems:

  • if your class has collections of related DB entities, extra queries will be made to load those
  • if you have bidirectional relation in classes, you can experience a stack overflow
  • to prevent this behavior, you will need to specify the serialization somehow (eg using some @Json* annotations if you serialize to JSON)
  • if your class only contains IDs, you're fine (but you are losing a lot of Hibernate's goodness)

To understand those problems, you need to know how Hibernate actually works.

The enhancement allows the entity to be partially loaded. Eg, you want to get all books for given user. You get a collection of books, but what you really have is a collection of hibernate-enhanced classes , that only wrap IDs at the moment. Provided that you use the default lazy loading.

Unless you really need something else than IDs, the data will never be loaded. Upon a getter call, background queries are made to obtain the extra information, if needed.

Now, imagine a user has a collection of all his books, as a field. When lazily loaded from DB, there may be nothing at all. However, if you want to serialize it, all getters are called and you get all the books, and transitively , every book author, should the books have a relation to its authors ...

If the relation is bidirectional in the classes, you can even create a cycle that will cause a stack overflow error, where you look who's the book's owner and then you fetch books for him again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM