0

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?

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • Java serialization and databases provide different ways of addressing the same problem: making information persist. Hibernate doesn't rely on serialization (or at least, not if you use it properly), so serialization is not "taken care of". – Stephen C Jun 29 '15 at 02:57
  • i would say you are better talking about JPA-Java persistence API [set of rules that hibernate, eclipselink, apache derby etc follow !] a lot of the class members that are to be persisted must implement serializeable... **http://stackoverflow.com/questions/2020904/when-and-why-jpa-entities-should-implement-serializable-interface** – Srinath Ganesh Jun 29 '15 at 02:58
  • some JPA spec mentioned here http://stackoverflow.com/a/2021640/1897935 "If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface." => if the class implements it then ALL fields must also be serialisable – Srinath Ganesh Jun 29 '15 at 03:00
  • @StephenC Hmmm, how does hibernate persist objects then? Wouldn't it need to make use of something like ObjectOutputStream, but just without telling us? – Grateful Jun 29 '15 at 03:02
  • @SrinathGanesh - What JPA is talking about there is passing objects around, not persisting them. – Stephen C Jun 29 '15 at 03:03
  • @Grateful - It puts them into an SQL database, using SQL insert or update statements. It uses JDBC ... not `ObjectOutputStream`. – Stephen C Jun 29 '15 at 03:04
  • JPA defines a set of rules that hibernate, eclipselink, apache derby etc follow https://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA%3F – Srinath Ganesh Jun 29 '15 at 03:04
  • @StephenC Whoa... wait a minute please. I think we are confusing two different things here. ObjectOutputStream is NOT a replacement for JDBC!! ObjectOutputStream helps to convert objects into a sequence of bits that can THEN be persisted into a database using JDBC. – Grateful Jun 29 '15 at 03:11
  • I never said the it was. The point is that Hibernate *doesn't* put serialized objects into the database unless you tell it to. And telling it to is a bad idea. By contrast ... *"I will try to do more research before I return to this thread."* ... would be a good idea :-) – Stephen C Jun 29 '15 at 05:02
  • @StephenC Thank you. – Grateful Jun 29 '15 at 08:51

2 Answers2

3

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.

Community
  • 1
  • 1
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • I am confused by your answer. Using JDBC does not mean that serialisation is not used. Anyway, my question was not intended to be about 'reading and writing an Object to a database'. I just wanted to know how hibernate converts the objects to a persist-able state? I mean obviously, it would need to convert the object to a more database-friendly state before it can be saved in a database. – Grateful Jun 29 '15 at 03:18
  • It uses `UserType`s as I explained – dkatzel Jun 29 '15 at 03:22
  • Okay, I think my lack of knowledge is preventing me from completely understanding this at the moment. I will try to do more research before I return to this thread. Thanks for your time though. – Grateful Jun 29 '15 at 03:27
1

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 (e.g. 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. E.g., 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.

Vlasec
  • 5,500
  • 3
  • 27
  • 30