简体   繁体   中英

Why JPA-2.0 Primary Key Classes have to implement Serializable but my example works without?

In many sources I have read PrimaryKey Classes and even JPA2 entities should be serializable.

IN my example (legacy database) there is a relationship between employee and languages:

Employee Class:

@Entity
@IdClass(EmpleadoId.class)
@Table(name = "NO_INFGRAEMPL")
public class Empleado {    

  @Id
  @Column(name = "IGECOMPANIA", unique = true)
  private String compania;

  @Id
  @Column(name = "IGENUMEROIDENTIFIC", unique = true)
  private String numeroIdentificacion;

//...
}

Employee Compound PrimaryKey Class:

public class EmpleadoId  {

  private String compania;
  private String numeroIdentificacion;
  //...         
}

Employee Language SKill Class:

@Entity
@IdClass(IdiomaEmpleadoId.class)
@Table(name = "NO_IDIOMEMPLE")
public class IdiomaEmpleado {

  @Id
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumns(value = { 
      @JoinColumn(name= "IEMCOMPANIA", referencedColumnName = "IGECOMPANIA"), 
      @JoinColumn(name = "IEMEMPLEADO", referencedColumnName = "IGENUMEROIDENTIFIC")
      })
  private Empleado empleado;

  @Id
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "IEMIDIOMA")
  private Idioma idioma;

  @Column(name = "IEMNIVELLECTURA")
  private String nivelLectura;
//...
}

Employee Language Skill Compound PrimaryKey Class:

public class IdiomaEmpleadoId {

  private EmpleadoId empleado;
  private String idioma;    

  //...    
}

Language Class:

@Entity
@Table(name = "NO_IDIOMAS")    
public class Idioma {

  @Id
  @Column(name = "IDICODIGO")
  private String codigo;

  @Column(name = "IDIDESCRIPCION")
  private String descripcion;

//...
}

I am using EclipseLink JPA2 Provider under a J2SE application and it is not giving me any exceptions.

My questions are:

  • Why is it not giving me exceptions? Is it not enforced to have Serializable?
  • Is it safe to continue this way or should I definitely implemente serializable?.
  • In which ones?, JPA2 Entities or PrimaryKey Classes?

Thanks a lot for the help.

JPA specification contains such a requirement ( JSR-317 secion 2.4 Primary Keys and Entity Identity):

The primary key class must be serializable.

If EclipseLink really doesn't enforce this requirement, it's an implementation detail of EclipseLink and I wouldn't recommend you to rely on it.

However, there are no requirements on serializability of entities, except for the following one which looks more like a recommendation than a requirement:

If an entity instance is to be passed by value as a detached object (eg, through a remote interface), the entity class must implement the Serializable interface.

Nothing is required to be serializable, but it seems it is requried by the spec (10x to axtavt) for primary keys, although there is no direct need for it.

Serialization is needed if the objects are transferred over-the-wire or persisted to disk, so I can't see the reason behind that decision. However, you should conform to it.

Primary key classes have to implement serializable and composite-ID class must implement serializable are two different questions. I am going to answer you both, and hope it will help you to distinguish and understand holistically.

  1. Primary key classes have to implement serializable: Note: It could work without its iplementation also. JPA specification contains such a requirement (JSR-317 secion 2.4 Primary Keys and Entity Identity):

    The primary key class must be serializable.

    However, there are no requirements on serializability of entities, so it's a recommendation than a requirement exception: If an entity instance is to be passed by value as a detached object (eg, through a remote interface), the entity class must implement the Serializable interface.

  2. Composite-ID class must implement serializable. The id is used as a key to index loaded objects in the session. The session object needs to be serializable, hence all objects referenced by it must be serializable as well. In case of CompositeIds the class itself is used as the id.

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