JPA problems persisting cascading a subclass

3

I have the following classes

@Entity
@Table(name = "personas")
@Inheritance(strategy = InheritanceType.JOINED)
@ClassExtractor(PersonExtractor.class)
abstract public class Persona implements AbstractEntity<Long>, Serializable {

  private static final long serialVersionUID = -1584956755146844000L;      

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id_persona")
  private long id;

  ...
}


@Entity
@Table(name = "personas_fisicas")
@PrimaryKeyJoinColumn(name = "id_persona")
public class PersonaFisica extends Persona {

   ...
}

@Entity
@Table(name = "legajos_empleados")
public class LegajoEmpleado implements AbstractEntity<Long>, Serializable {

   private static final long serialVersionUID = 4528140042468695166L;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "id_legajo_empleado")
   private Long id;

   @OneToOne(cascade = {CascadeType.ALL},optional = false)
   @JoinColumn(name = "empleado", referencedColumnName = "id_persona", nullable = false)
   private PersonaFisica empleado;

   ...
}

The problem is that when I persist an instance of LegajoEmpleado that has an instance of PersonaFisica in the "employee" attribute that is not registered in the database, only a record is generated for Person and not for PersonaFisica and when it tries to save the file in the bd is a violation of the FK.

Detallado:   INSERT INTO personas (celular, cliente, codigo_postal, direccion, email, localidad, observaciones, pais, proveedor, provincia, telefono) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [, false, , , , , , , false, , ]
Detallado:   select lastval()
Detallado:   INSERT INTO legajos_empleados (fecha_inicio_categoria, fecha_inicio_empresa, fecha_inicio_periodo, legajo_interno, OBSERVACIONES, valor_hora_adicional, valor_hora_escalafon, categoria_actual, empleado) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [2018-03-30, 2018-03-15, 2018-03-15, , , 0.0, 0.0, 2, 19]
Detallado:   SELECT 1
Advertencia:   Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: inserción o actualización en la tabla «legajos_empleados» viola la llave foránea «legajos_empleados_empleado_fkey»
Detail: La llave (empleado)=(19) no está presente en la tabla «personas_fisicas».
Error Code: 0

Edition:

Tables

    
asked by MEG 02.03.2018 в 00:17
source

1 answer

2

The OneToOne relationship must be done directly with the Person entity, since it is the entity that owns the relationship and the one with the id. Then you can pass the PersonaFisica entity or, if you need it, anyone who inherits Persona.

That is, in the LegajoEmpleado entity, the relationship would be the following:

@OneToOne(cascade = {CascadeType.ALL},optional = false)
@JoinColumn(name = "empleado", referencedColumnName = "id_persona", nullable = false)
private Persona empleado;

And then in your code you can do this perfectly:

empleado = new PersonaFisica();
empleado.set... // los que sean
entityManager.persist(legajoEmpleado);
    
answered by 02.03.2018 / 01:25
source