Я пытаюсь создать сопоставления JPA для следующих таблиц базы данных:
Поля ID_CODA, ID_MOVEMENT и ID_INFORMATION генерируются последовательностями в базе данных, и я хотел бы сохранить весь объект CODA в базе данных в одной транзакции.
Вот что я уже сделал:
@Entity
@Table(name = "CODA_TEST")
public class CodedAccountStatement implements Serializable {
@Id
@Column(name = "ID_CODA")
@SequenceGenerator(name = "seq_coda", sequenceName = "seq_coda", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_coda")
private long identifier;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "statement")
private List<MovementRecord> movements;
public void addMovement(MovementRecord movementRecord) {
if(this.movements == null) {
this.movements = new ArrayList<>();
}
movementRecord.setStatement(this);
this.movements.add(movementRecord);
}
@Entity
@Table(name = "CODA_ARTICLE_MOUVEMENT_TEST")
@IdClass(MovementRecordIdentifier.class)
public class MovementRecord implements Serializable {
@Id
@Column(name = "ID_MOUVEMENT")
@SequenceGenerator(name = "seq_coda_article_mouvement", sequenceName = "seq_coda_article_mouvement",allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_coda_article_mouvement")
private long movementId;
@Id
@ManyToOne
@JoinColumn(name = "ID_CODA", referencedColumnName = "ID_CODA")
private CodedAccountStatement statement;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "movementRecord")
private DomiciliationRecord domiciliationRecord;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "movementId")
private List<InformationRecord> informationRecords;
public void setDomiciliationRecord(DomiciliationRecord domiciliationRecord) {
domiciliationRecord.setMovementRecord(this);
this.domiciliationRecord = domiciliationRecord;
}
public void addInformationRecord(InformationRecord informationRecord) {
if(this.informationRecords == null) {
this.informationRecords = new ArrayList<>();
}
informationRecord.setMovementId(this);
this.informationRecords.add(informationRecord);
}
@Entity
@IdClass(InformationRecordIdentifier.class)
@Table(name = "CODA_ARTICLE_INFORMATION_TEST")
public class InformationRecord implements Serializable {
@Id
@ManyToOne
@JoinColumns({
@JoinColumn(name = "ID_MOUVEMENT", referencedColumnName = "ID_MOUVEMENT"),
@JoinColumn(name = "ID_CODA", referencedColumnName = "ID_CODA")
})
private MovementRecord movementId;
@Id
@Column(name = "ID_INFORMATION")
@SequenceGenerator(name = "seq_coda_article_information", sequenceName = "seq_coda_article_information", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_coda_article_information")
private long informationId;
@Entity
@IdClass(MovementRecordIdentifier.class)
@Table(name = "CODA_ARTICLE_MOUVEMENT_DOM_TEST")
public class DomiciliationRecord implements Serializable {
@Id
@Column(name = "ID_MOUVEMENT")
private long movementId;
@Id
@Column(name = "ID_CODA")
private long statement;
@OneToOne
@JoinColumns({
@JoinColumn(name = "ID_MOUVEMENT"),
@JoinColumn(name = "ID_CODA")
})
private MovementRecord movementRecord;
А вот как я могу сохранить этот объект, используя Spring Data Jpa
интерфейс:
private final CodedAccountStatementRepository repository;
@Transactional
public void save() {
CodedAccountStatement statement = new CodedAccountStatement();
MovementRecord movementRecord = new MovementRecord();
movementRecord.setDomiciliationRecord(new DomiciliationRecord());
movementRecord.addInformationRecord(new InformationRecord());
statement.addMovement(movementRecord);
LOGGER.info("Coda to save = {}", statement);
repository.save(statement);
}
А как видно из журналов:
insert into CODA_TEST (ID_CODA) values (?)
binding parameter [1] as [BIGINT] - [140]
insert into CODA_ARTICLE_MOUVEMENT_TEST (ID_MOUVEMENT, ID_CODA) values (?, ?)
binding parameter [1] as [BIGINT] - [3400]
binding parameter [2] as [BIGINT] - [140]
insert into CODA_ARTICLE_INFORMATION_TEST (ID_INFORMATION, ID_MOUVEMENT, ID_CODA) values (?, ?, ?)
binding parameter [1] as [BIGINT] - [127]
binding parameter [2] as [BIGINT] - [3400]
binding parameter [3] as [BIGINT] - [140]
insert into CODA_ARTICLE_MOUVEMENT_DOM_TEST (ID_MOUVEMENT, ID_CODA) values (?, ?)
binding parameter [1] as [BIGINT] - [0]
binding parameter [2] as [BIGINT] - [0]
ORA-02291: integrity constraint (EGCA1.FK_CODA_ARTICLE_MOUVEMENT_DOM_CODA_TEST) violated - parent key not found
Идентификаторы были сгенерированы правильно для каждой сущности, но Hibernate не дает правильные значения для первичного ключа DomiciliationRecord
(у нас 0, 0) ...