We have the following two tables linked to a one-to-one relationship with a shared primary key:
CREATE TABLE "PERSONAL"."INASISTENCIAS" (
"**ID_INASISTENCIA**" INTEGER NOT NULL,
"FECHA_COMIENZO" DATE NOT NULL,
"FECHA_FIN" DATE NOT NULL,
"CANTIDAD_DIAS_SOLICITADOS" INTEGER NOT NULL,
)
CREATE TABLE "PERSONAL"."INASISTENCIAS_ESTUDIO" (
"ID_INASISTENCIA_ESTUDIO" INTEGER NOT NULL,
"FECHA_PRUEBA" DATE NOT NULL,
"INSTITUCION_PRUEBA" VARCHAR(50) NOT NULL,
"MATERIA" VARCHAR(50) NOT NULL,
)
ALTER TABLE "PERSONAL"."**INASISTENCIAS**" ADD CONSTRAINT "INASISTS_PK" **PRIMARY KEY ("ID_INASISTENCIA")**;
ALTER TABLE "PERSONAL"."**INASISTENCIAS_ESTUDIO**" ADD CONSTRAINT "INASISTSESTUD_PK" **PRIMARY KEY ("ID_INASISTENCIA_ESTUDIO")**;
ALTER TABLE "PERSONAL"."INASISTENCIAS_ESTUDIO" ADD CONSTRAINT "INASISTSESTUD_INASISTS_FK" **FOREIGN KEY
("ID_INASISTENCIA_ESTUDIO")
REFERENCES "PERSONAL"."INASISTENCIAS"
("ID_INASISTENCIA")**;
The corresponding JPA entities:
@Entity
@Table(name="INASISTENCIAS", schema="PERSONAL")
public class **Inasistencia** implements Serializable {
private int **idInasistencia**;
private Date fechaComienzo;
private Date fechaFin;
private int cantidadDiasSolicitados;
public Inasistencia() {
}
**@Id**
**@SequenceGenerator**(name="INASISTENCIAS_IDINASISTENCIA_GENERATOR",
sequenceName="PERSONAL.SEQ_INASISTENCIAS")
@GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="INASISTENCIAS_IDINASISTENCIA_GENERATOR")
@Column(name="ID_INASISTENCIA")
public int getIdInasistencia() {
return this.**idInasistencia**;
}
public void setIdInasistencia(int idInasistencia) {
this.idInasistencia = idInasistencia;
}
//uni-directional one-to-one association to InasistenciasEstudio
@**OneToOne**(cascade={CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH}, fetch=FetchType.LAZY)
@JoinColumn(name="ID_INASISTENCIA",
referencedColumnName="ID_INASISTENCIA_ESTUDIO", nullable=true,
updatable=false, insertable=false)
public InasistenciaEstudio getInasistenciasEstudio() {
return this.**inasistenciasEstudio**;
}
public void setInasistenciasEstudio(InasistenciaEstudio
inasistenciasEstudio) {
this.inasistenciasEstudio= inasistenciasEstudio;
}
....
}
@Entity
@Table(name="INASISTENCIAS_ESTUDIO", schema="PERSONAL")
public class **InasistenciaEstudio** implements Serializable {
private int **idInasistenciaEstudio**;
private Date fechaPrueba;
private String institucionPrueba;
private String materia;
public InasistenciaEstudio() {
}
@**Id**
@Column(name="ID_INASISTENCIA_ESTUDIO", unique=true, nullable=false)
public int getIdInasistenciaEstudio() {
return this.**idInasistenciaEstudio**;
}
public void setIdInasistenciaEstudio(int idInasistenciaEstudio) {
this.idInasistenciaEstudio = idInasistenciaEstudio;
}
....
}
Absences and InaistenciesStudy share the primary key whose value is automatically generated by the engine OPenJPA (SNAPSHOT_2.1.2) at the time of the committee using the sequence defined in the entity Inability (SEQ_INASISTENCIAS).
The problem is that the value of the key in insistence.idInsistence is not propagated to non-attendance.StudyStudy.idInsistenceStudy when both related entities are persisted. As we understand from the JPA documentation this should happen automatically without additional annotations.
Test code:
Inasistencia inasis = new Inasistencia();
*<set Inasistencia att..>*
InasistenciaEstudio inasisEstudio = new InasistenciaEstudio();
*<set InasistenciaEstudio att..>*
inasis.setInasistenciasEstudio(inasisEstudio);
entityManager.saveObject(inasis);
trace trace of the test:
2132 LicensesDomain TRACE [main] openjpa.jdbc.SQL - executing prepstmnt -928506975 SELECT t0.USUARIO_ULTIMA_MODIFICACION, t0.FECHA_PRUEBA, t0.INSTITUTION_PRUEBA, t0.materia, t0.MOMENTO_BAJA, t0.TIPO_PRUEBA FROM PERSONAL.STUDIOUSNESSES t0 WHERE t0.ID_INSTUDENT_STUDY =? optimize for 1 row [params = (int) 0] 2178 LicensesDomain TRACE [main] openjpa.jdbc.SQL - [46 ms] spent 2199 TRACE LicensesDomain [main] openjpa.jdbc.SQL - executing prepstmnt 1867579702 VALUES NEXTVAL FOR PERSONAL.SEQ_INASISTENCES
2201 TRACE LicensesDomain [main] openjpa.jdbc.SQL - [2 ms] spent 2221 LicensesDomain TRACE [main] openjpa.jdbc.SQL - executing prepstmnt 430574027 INSERT INTO PERSONAL. UNSISTENCES (ID_INASISTENCIA, USUARIO_ULTIMA_MODIFICACION, QUANTITY_DAYS_SOLICITED, DATE_COMMENT, DATE_FIN, ID_ROL_PERSONAL, ID_TIPO_INASISTENCIA, LIBRE_REFERENCIA, MOMENTO_BAJA, ID_REGIMEN_TRABAJO) VALUES (?,?,?,?,?,?,?,?,?,?,?) [params = (int) 90 , (String) TestEntities, (int) 10, (Date) 2016-07-28, (Date) 2016-08-07, (int) 271465, (int) ) 1, (Date) 2016-07-28, (null) null, (int) 1] 2356 LicensesDomain TRACE [main] openjpa.jdbc.SQL - [134 ms] spent 2357 LicensesDomain TRACE [main] openjpa.jdbc.SQL - executing prepstmnt 1692880180 PERSONAL INSERT INTO. UNSISTENCES_STUDY (STUDENT_INSTUDENT_ID, USUARIO_ULTIMA_MODIFICACION, DATE_PRUEBA, INSTITUCION_PRUEBA, subject, MOMENTO_BAJA, TIPO_PRUEBA) VALUES (?,?,?,?,?,?,?) [params = (int) 0 , (String) TestEntities, (Date) 2016-08-07, (String) Udelar, (String) Mathematical, (null) null, (String) Partial] 2374 LicensesDomain TRACE [main] openjpa.jdbc.SQL - [17 ms] spent