Thiru
Thiru

Reputation: 799

Why do POJO classes need to implement the Serializable interface?

Why do POJO Java classes have to implement the Serializable interface? What will happen if I do not implement Serializable?

@Entity
@Table(name="Customer")
public class Customer implements Serializable {

    private static final long serialVersionUID = -5294188737237640015L;
    /**
     * Holds Customer id of the customer
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "cust_id")
    private int Custid;

    /** Holds first Name of the customer*/
    @Column(name = "first_name")
    private String firstName;

    /** Holds last Name of the customer */
    @Column(name = "last_name")
    private String lastName;

    /** Holds Customer mobile number of customer*/
    @Column(name = "cust_mobile")
    private long MobileNo;

    /**Holds customer address of customer*/
    @Column(name = "cust_address")

Upvotes: 8

Views: 18574

Answers (2)

Frank
Frank

Reputation: 2547

The term "POJO" was invented to seperate between lightweight and heavyweight EJB concepts.

POJOs do not require you to implement anything.

It was used for objects that must NOT implement heavyweight EJB interfaces.

The term became obsolete with the introduction of JEE5 and the subsequent use of lightweight annotations for the definition of EJBs.

From my expirience I would say that the term is now toxic and should not be used anymore. Nowadays it is just a common source of misunderstandings and unnecessary discussions.

BTW: I believe you are referring to "JavaBeans".

Upvotes: 0

shazin
shazin

Reputation: 21923

First this is no longer a Plain Old Java Object, Because it has annotations.

But staying on your premise, The Serializable is required in POJOs if those are intended to be used in Distributed Systems and Systems which use caching and flushing into files and reading back.

Mostly JPA implementations do run in Distributed manner and Use caching, thus this POJO is required to implement Serializable.

For more information read this discussion

Upvotes: 10

Related Questions