[英]hibernate: persist error with detached entity
我有一个我不明白的问题。 我有几个实体:
场景实体
@Entity
@Table(name = "scenario")
public class Scenario {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "scenario_id")
private int id;
@Column(name = "title", nullable = false)
private String title;
@NotNull
@DateTimeFormat(pattern = "dd/MM/yyyy")
@Column(name = "creation_date", nullable = false)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate creationDate;
@OneToMany(mappedBy = "scenario")
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Section> sectionList = new HashSet<Section>();
@ManyToOne
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "id", nullable = false)
private User user;
@OneToMany(mappedBy = "scenario", orphanRemoval=true)
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Plot> plotList = new HashSet<Plot>();
绘图实体
@Entity
@Table(name = "Plot")
public class Plot {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "plot_id")
private int id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "description")
private String description;
@ManyToOne
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "id", nullable = false)
private Scenario scenario;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "plot_role", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = {
@JoinColumn(name = "plot_id") })
private Set<Role> roles = new HashSet<Role>();
和控制器
@Autowired
UserService userService;
@Autowired
ScenarioService scenarioService;
@Autowired
PlotService plotService;
@RequestMapping(value = { "/scenario-{id}-newPlot" }, method = RequestMethod.GET)
public String newPlot(ModelMap model, @PathVariable int id) {
Plot plot = new Plot();
Scenario scenario = scenarioService.findById(id);
model.addAttribute("scenario", scenario);
model.addAttribute("plot", plot);
model.addAttribute("edit", false);
return "plotform";
}
@RequestMapping(value = { "/scenario-{id}-newPlot" }, method = RequestMethod.POST)
public String savePlot(@Valid Plot plot, BindingResult result, ModelMap model, @PathVariable int id) {
if (result.hasErrors()) {
System.out.println(result.toString());
return "plotform";
}
model.addAttribute("success",
"Plot " + plot.getName() + " created successfully");
plot.setScenario(scenarioService.findById(id));
System.out.println("Plot " + plot.toString());
plotService.savePlot(plot);
return "success";
}
我也有服务,手段和形式。 问题是,当我尝试从表单中保存plot
时,我得到: Request processing failed; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.btw.spindle.model.Plot
Request processing failed; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.btw.spindle.model.Plot
我不明白它是如何分离的以及如何修复它。 我累了添加System.out
来检查是否从表单中正确提取了必要数据。 而且我还有用户实体(在此未添加)。 之间的关系user
和scenario
是一样的(一对多)的之间的scenario
和plot
。 scenario
创建可以完美地进行,并且plots
创建会抛出此持久异常。
有什么提示吗?
导致此异常的原因有多种。 此异常背后的基本思想是,您要持久存储的对象未处于持久状态。
您可以在保存或保留ID之前检查Plot对象是否已占用ID吗?
编辑
您解决了,完美。
Ok MeewU是正确的。 问题在于,对象在保留之前具有设置的ID。 原因是:
@RequestMapping(value = { "/scenario-{id}-newPlot" }, method = RequestMethod.GET)
public String newPlot(ModelMap model, @PathVariable int id) {
我使用它来传递场景ID,以在下一步将ot添加到绘图中。 但事实证明,方案的ID已自动设置为地块ID。 我将变量名称更改为“方案”,并且不再设置ID,并且按计划进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.