Fre*_*red 5 rest spring transactions spring-data spring-data-rest
我正在使用 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;
}
Run Code Online (Sandbox Code Playgroud)
因此,我希望能够同时(在同一事务中)保存(创建和更新)整个场景及其操作和操作参数。
使用 Spring Data REST 实现这一目标的最佳方法是什么?
更新 1:
我尝试按照建议使用级联属性,但现在出现此错误:
无法写入内容:检测到多个具有相同关系类型的关联链接!消歧关联@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在注释中使用属性@OneToMany:
IE:
@OneToMany(mappedBy = "scenario", cascade = CascadeType.MERGE)
@LazyCollection(LazyCollectionOption.TRUE)
@JsonManagedReference
private Set<Action> actions = new HashSet<Action>();
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以将Action实例添加到预定义中Set并将它们保存在您的Scenario
Scenario scenario = new Scenario():
scenario.getActions.add(new Action);
scenarioRepository.save(scenario);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1893 次 |
| 最近记录: |