Но когда я пытаюсь удалить идентификатор Childern, я получаю Entity2, была изменена с 3 на NULL. Родитель и Чилдерн, как только я сохраняю родителя как родителя, так и детей, которые будут сохранены, поскольку новые записи и идентификаторы будут автоматически заполнены. < /P>
Код: Выделить всё
@Table(name = "ENTITIES")
public class Entity2 extends BaseEntity {
@Column(name="PARENT_ID" ) private Long parentId;
@Column(name="POPUP_JSON_STYLING" ) private String popupJsonStyling;
@Column(name="SHOW_IS_LOCKED" ) private Boolean showIsLocked;
//RELATIONS (For Copy Entities)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parentId")
@Where(clause = "active = true and is_deleted = false")
private List childEntities;
}
@Transactional
public void copyEntityList(List ids) {
//GET ENTITIES
List entities = entityRepository.findAllById(ids);
//REMOVE IDS, CHANGE NAME & CODE
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
for(Entity2 entity : entities) {
entity.setId (null);
entity.setName(entity.getName() + " - COPY (" + LocalDateTime.now().format(formatter) + ")");
entity.setCode(entity.getCode() + " (" + LocalDateTime.now().format(formatter) + ")");
for(Entity2 childEntity : entity.getChildEntities()) {
childEntity.setId(null);
}
}
//INSERT ENTITIES
entityRepository.saveAll(entities);
//COPY CHILD ENTITIES
//copyChildEntities(entities);
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... d-children