3

I'm doing a tutorial roseindia.net (http://www.roseindia.net/tutorial/java/jdbc/dataaccessobjectdesignpattern.html) and to make the bean StudentBean.java implement java.io.Serializable interface.

Does this have any reason? Is the data stored differently in the database to implement Serializable interface?

Arun Kumar
  • 6,534
  • 13
  • 40
  • 67
  • check this http://stackoverflow.com/questions/2020904/when-and-why-jpa-entities-should-implement-serializable-interface – NPKR Sep 28 '12 at 04:58
  • 1
    You are doing a tutorial from perhaps the most notorious site of programming misinformation on the Internet. Please try to find a better one. – user207421 Sep 28 '12 at 07:18

2 Answers2

2

The tutorial you linked to does not make use of the class being Serializable.

They might have thrown it in there to follow a general Java bean pattern, but it is not needed for writing it to a database via JDBC. You are rightly confused about this, because there is no explanation.

Serializable is used for ObjectOutputStream, for example to write to files or transfer the bean over the network using RMI. So for a data transport object, this is not a bad feature to support.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I understand, excellent response, very successful for me. I just have one more question: I could store the state of my object in the DB as a set of bits implementing serializable interface? I say this in case you want to store an object with attributes more complex than simple strings or integers (such as a map or an arraylist) – felipecamposclarke Sep 28 '12 at 07:16
  • 1
    Yes, you could turn a Serializable object into a byte[] (binary data) and then put that it the DB. But then it cannot be queried in SQL anymore (because the database has no idea what those bytes mean). – Thilo Sep 28 '12 at 07:21
-1

Java provides classes to support writing object state to streams and restoring objects from streams. java.io.Serializable is an Marker Interface which provides ability to selialize the object's state of your class(which implements Serializable) in DB table,Files or to transfer over network etc.

Arun Kumar
  • 6,534
  • 13
  • 40
  • 67
  • 2
    No, it's not. JDBC setString/setInt is used to store the object state. – Thilo Sep 28 '12 at 05:32
  • I understand that it is possible to save the state of an object through seriazisable interface (through the transformation of this a set of bits), but in the link you put above, the attributes of the class to persist are stored in the database like any other data (could have done the same thing without the serializable interface) am I right? – felipecamposclarke Sep 28 '12 at 07:13