为什么POJO类需要实现Serializable接口?

Thi*_*iru 6 java

为什么POJO Java类必须实现Serializable接口?如果我不执行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")
Run Code Online (Sandbox Code Playgroud)

sha*_*zin 9

首先,它不再是普通的旧Java对象,因为它有注释.

但是,如果要在那些使用缓存和刷新文件并回读的分布式系统和系统中使用,那么在POJO中需要Serializable.

大多数JPA实现都以分布式方式运行并使用缓存,因此需要此POJO来实现Serializable.

有关更多信息,请阅读此讨论