Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class BranchCreationEntity extends EquipmentCreationEntity {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(
joinColumns = @JoinColumn(name = "branch_id"), foreignKey = @ForeignKey(name = "branch_id_fk"),
inverseJoinColumns = @JoinColumn(name = "operational_limits_groups_id"), inverseForeignKey = @ForeignKey(name = "operational_limits_groups_id_fk"))
inverseJoinColumns = @JoinColumn(name = "operational_limits_groups_id"), inverseForeignKey = @ForeignKey(name = "operational_limits_groups_creation_id_fk"))
@OrderColumn(name = "pos_operationalLimitsGroups")
private List<OperationalLimitsGroupEntity> operationalLimitsGroups;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class VscCreationEntity extends EquipmentCreationEntity {
name = "converter_station_1_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(
name = "converter_station_1_id_fk"
name = "converter_station_creation_1_id_fk"
))
private ConverterStationCreationEntity converterStation1;

Expand All @@ -69,7 +69,7 @@ public class VscCreationEntity extends EquipmentCreationEntity {
name = "converter_station_2_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(
name = "converter_station_2_id_fk"
name = "converter_station_creation_2_id_fk"
))
private ConverterStationCreationEntity converterStation2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class BranchModificationEntity extends BasicEquipmentModificationEntity {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(
joinColumns = @JoinColumn(name = "branch_id"), foreignKey = @ForeignKey(name = "branch_modification_id_fk"),
inverseJoinColumns = @JoinColumn(name = "operational_limits_groups_id"), inverseForeignKey = @ForeignKey(name = "operational_limits_groups_id_fk"))
inverseJoinColumns = @JoinColumn(name = "operational_limits_groups_id"), inverseForeignKey = @ForeignKey(name = "operational_limits_groups_modification_id_fk"))
@OrderColumn(name = "pos_operationalLimitsGroups")
private List<OperationalLimitsGroupModificationEntity> operationalLimitsGroups;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gridsuite.modification.dto.ModificationInfos;
import org.gridsuite.modification.server.entities.ModificationEntity;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -25,10 +26,9 @@ public class EquipmentModificationEntity extends ModificationEntity {
@Column(name = "equipmentId")
private String equipmentId;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "equipment_modification_id")
@OneToMany(mappedBy = "modification", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@OrderColumn(name = "insert_position")
private List<FreePropertyEntity> properties;
private List<FreePropertyEntity> properties = new ArrayList<>();

protected EquipmentModificationEntity(EquipmentModificationInfos equipmentModificationInfos) {
super(equipmentModificationInfos);
Expand All @@ -45,16 +45,19 @@ private void assignAttributes(EquipmentModificationInfos equipmentModificationIn
equipmentId = equipmentModificationInfos.getEquipmentId();
List<FreePropertyEntity> newProperties = equipmentModificationInfos.getProperties() == null ? null :
equipmentModificationInfos.getProperties().stream()
.map(FreePropertyEntity::new)
.toList();
.map(info -> {
FreePropertyEntity entity = new FreePropertyEntity(info);
entity.setModification(this);
return entity;
}).toList();
if (this.properties != null) {
// update using the same reference with clear/add (to avoid JPA exception)
this.properties.clear();
if (newProperties != null) {
this.properties.addAll(newProperties);
}
} else {
this.properties = newProperties;
this.properties = (newProperties != null) ? new ArrayList<>(newProperties) : new ArrayList<>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.gridsuite.modification.dto.FreePropertyInfos;
import org.gridsuite.modification.server.entities.ModificationEntity;

import java.util.UUID;
import org.gridsuite.modification.dto.FreePropertyInfos;

/**
* @author Joris Mancini <joris.mancini_externe at rte-france.com>
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
Expand All @@ -46,6 +49,10 @@ public class FreePropertyEntity {
@Column(name = "previous_value")
private String previousValue;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "equipment_modification_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "free_property_modification_id_fk_constraint"))
private ModificationEntity modification;

public FreePropertyInfos toInfos() {
return FreePropertyInfos.builder()
.name(name)
Expand All @@ -57,6 +64,7 @@ public FreePropertyInfos toInfos() {
}

public FreePropertyEntity(FreePropertyInfos freePropertyInfos) {
this(null, freePropertyInfos.getName(), freePropertyInfos.getValue(), freePropertyInfos.isDeletionMark(), freePropertyInfos.isAdded(), freePropertyInfos.getPreviousValue());
this(null, freePropertyInfos.getName(), freePropertyInfos.getValue(), freePropertyInfos.isDeletionMark(),
freePropertyInfos.isAdded(), freePropertyInfos.getPreviousValue(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class VscModificationEntity extends BasicEquipmentModificationEntity {
name = "converter_station_1_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(
name = "converter_station_1_id_fk"
name = "converter_station_modification_1_id_fk"
))
private ConverterStationModificationEntity converterStation1;

Expand All @@ -113,7 +113,7 @@ public class VscModificationEntity extends BasicEquipmentModificationEntity {
name = "converter_station_2_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(
name = "converter_station_2_id_fk"
name = "converter_station_modification_2_id_fk"
))
private ConverterStationModificationEntity converterStation2;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="rehiligha (generated)" id="1761058240544-39">
<addForeignKeyConstraint baseColumnNames="id" baseTableName="battery_creation" constraintName="batteryCreation_id_fk_constraint" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="modification" validate="true"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-40">
<addForeignKeyConstraint baseColumnNames="converter_station_1_id" baseTableName="vsc_modification" constraintName="converter_station_modification_1_id_fk" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="converter_station_modification" validate="true"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-41">
<addForeignKeyConstraint baseColumnNames="converter_station_2_id" baseTableName="vsc_modification" constraintName="converter_station_modification_2_id_fk" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="converter_station_modification" validate="true"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-42">
<addForeignKeyConstraint baseColumnNames="operational_limits_groups_id" baseTableName="line_modification_operational_limits_groups" constraintName="operational_limits_groups_line_modification_id_fk" deferrable="false" initiallyDeferred="false" referencedColumnNames="uuid" referencedTableName="operational_limits_group_modification" validate="true"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-43">
<addForeignKeyConstraint baseColumnNames="operational_limits_groups_id" baseTableName="two_windings_transformer_modification_operational_limits_groups" constraintName="operational_limits_groups_2wt_modification_id_fk" deferrable="false" initiallyDeferred="false" referencedColumnNames="uuid" referencedTableName="operational_limits_group_modification" validate="true"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-44">
<dropForeignKeyConstraint baseTableName="TABULAR_PROPERTY" constraintName="FK1t4r9etm29snityophivgnkce"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-45">
<dropForeignKeyConstraint baseTableName="FREE_PROPERTY" constraintName="FK3wbc92cp7wlvba202qx7ldy6n"/>
</changeSet>
<changeSet author="rehiligha (generated)" id="1761058240544-46">
<addForeignKeyConstraint baseColumnNames="equipment_modification_id" baseTableName="free_property" constraintName="free_property_modification_id_fk_constraint" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="modification" validate="true"/>
</changeSet>
</databaseChangeLog>
3 changes: 3 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,6 @@ databaseChangeLog:
- include:
file: changesets/changelog_20251104T150402Z.xml
relativeToChangelogFile: true
- include:
file: changesets/changelog_20251021T145023Z.xml
relativeToChangelogFile: true
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ void testSubstationCreation() {

SQLStatementCountValidator.reset();
networkModificationRepository.deleteModificationGroup(TEST_GROUP_ID, true);
assertRequestsCount(5, 0, 2, 4);
assertRequestsCount(5, 0, 1, 4);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onetomany
@joincolumn(name = "equipment_modification_id")
The OneToMany owns the FK since it's declared on the parent side
On delete:

  1. UPDATE free_property SET equipment_modification_id = NULL WHERE equipment_modification_id = :id;
    (because the FK is managed by the parent, so we must break it before the delete)
  2. DELETE FROM free_property WHERE id = ***;
  3. DELETE FROM modification WHERE id = ***;
    => Results in 2 UPDATEs (UPDATE generated before the DELETE)

With:
FreePropertyEntity:
@manytoone
@joincolumn(name = "equipment_modification_id")
private ModificationEntity modification;

EquipmentModificationEntity:
@onetomany(mappedBy = "modification")
private List properties;

=> The ManyToOne owns the FK
On delete:
DELETE FROM free_property WHERE equipment_modification_id = :id
(the FK is managed by the child, so no need to set it to NULL)

Since we have @OrderColumn(name = "insert_position") an UPDATE is generated to update and reindex the list before DELETE


assertThrows(NetworkModificationException.class, () -> networkModificationRepository.getModifications(TEST_GROUP_ID, false, true),
new NetworkModificationException(MODIFICATION_GROUP_NOT_FOUND, TEST_GROUP_ID.toString()).getMessage());
Expand Down