Spring Data REST - How to save parent object with children at the same time

I use Data REST to expose my entities, and I want to save (create and update) the parent object along with its children at the same time.

Here are my entities:

@Entity
@Table(name = "scenario")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Scenario extends AbstractAuditableEntity {

    @Id
    // Sequence name must be preceded by schema name.
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "scenarioIdSeq")
    @SequenceGenerator(name = "scenarioIdSeq", sequenceName = "scenario_id_seq", allocationSize = 1)
    @Column(unique = true, nullable = false, columnDefinition = "SERIAL")
    private Long id;

    @Version
    // Used by JPA for optimistic locking
    protected int version;

    @OneToMany(mappedBy = "scenario")
    @LazyCollection(LazyCollectionOption.TRUE)
    private Set<Action> actions = new HashSet<Action>();
}

@Entity
@Table(name = "action")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Action extends AbstractAuditableEntity {

    @Id
    // Sequence name must be preceded by schema name.
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "actionIdSeq")
    @SequenceGenerator(name = "actionIdSeq", sequenceName = "action_id_seq", allocationSize = 1)
    @Column(unique = true, nullable = false, columnDefinition = "SERIAL")
    private Long id;

    @Version
    // Used by JPA for optimistic locking
    protected int version;

    @RestResource(rel = "action_scenario")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "scenario_id", columnDefinition = "BIGINT", nullable = false)
    private Scenario scenario;

    @JsonIgnore
    @OneToMany(mappedBy = "action")
    @LazyCollection(LazyCollectionOption.TRUE)
    private Set<ActionParameter> parameters = new HashSet<ActionParameter>();
}

@Entity
@Table(name = "action_parameter")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ActionParameter extends AbstractAuditableEntity {

    @Id
    // Sequence name must be preceded by schema name.
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "actionParamIdSeq")
    @SequenceGenerator(name = "actionParamIdSeq", sequenceName = "action_parameter_id_seq", allocationSize = 1)
    @Column(unique = true, nullable = false, columnDefinition = "SERIAL")
    private Long id;

    @Version
    // Used by JPA for optimistic locking
    protected int version;

    @RestResource(rel = "parameter_action")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "action_id", columnDefinition = "BIGINT", nullable = false)
    private Action action;
}

Therefore, I want to be able to simultaneously save (create and update) the entire script along with its actions and action parameters (within the same transaction).

What would be the best way to achieve this using Spring Data REST?

Update 1:

I tried using the cascade property as suggested, but now I get this error:

. type! Disambiguate @Org.springframework.data.rest.core.annotation.RestResource( =, = , description=@org.springframework.data.rest.core.annotation.Description(value =), = action_scenario)

@RestResource (rel= "xxx" ), , ?!

- ?

+4
1

cascade @OneToMany:

I.e.:

@OneToMany(mappedBy = "scenario", cascade = CascadeType.MERGE)     
@LazyCollection(LazyCollectionOption.TRUE)
@JsonManagedReference
private Set<Action> actions = new HashSet<Action>();
}

, Action Set Scenario

Scenario scenario = new Scenario():
scenario.getActions.add(new Action);
scenarioRepository.save(scenario);
0

Source: https://habr.com/ru/post/1617314/


All Articles