gpt4 book ai didi

Spring Data REST - 如何同时保存父实体和子实体

转载 作者:行者123 更新时间:2023-12-01 06:09:44 37 4
gpt4 key购买 nike

我正在使用 Spring Data REST 来公开我的实体,并且我希望能够同时保存(创建和更新)父实体及其子实体。

这是我的实体:

@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;
}

因此,我希望能够同时(在同一事务中)保存(创建和更新)整个场景及其操作和操作参数。

使用 Spring Data REST 实现这一目标的最佳方法是什么?

更新 1:

我尝试按照建议使用级联属性,但现在出现此错误:

Could not write content: Detected multiple association links with same relation type! Disambiguate association @org.springframework.data.rest.core.annotation.RestResource(path=, exported=true, description=@org.springframework.data.rest.core.annotation.Description(value=), rel=action_scenario)



每个关系都已经用 @RestResource(rel = "xxx") 注释了,所以我不明白为什么我会收到这个错误?!

我错过了什么吗?

最佳答案

您必须使用 cascade您的属性(property)@OneToMany注释:

IE。:

@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);

关于Spring Data REST - 如何同时保存父实体和子实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902919/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com