我有一个我不明白的问题。我有几个实体:
情景实体
@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
我不知道它是如何分离以及如何解决它。我累了添加System.out
来检查是否正确地从表单中提取了必要的数据。而且我还有用户实体(我没有在这里添加)。 user
和scenario
之间的关系与scenario
和plot
之间的关系相同(一对多)。 scenario
的创建工作完美,plots
的创建会引发此持续异常。
任何提示?
答案 0 :(得分:1)
有多种原因可能导致此异常。 此异常背后的基本思想是您尝试持久化的对象不处于持久状态。
在保存或保存之前,你能检查一下Plot对象是否有ID吗?
修改强>
你解决了,完美。
答案 1 :(得分:1)
Ok MeewU是对的。问题是因为该对象在被持久化之前具有设置ID。原因是:
@RequestMapping(value = { "/scenario-{id}-newPlot" }, method = RequestMethod.GET)
public String newPlot(ModelMap model, @PathVariable int id) {
我用它来传递场景ID,以便在下一步添加到场景中。但事实证明,场景的ID被自动设置为情节ID。我将变量名称更改为“scenario”,并且不再设置id并且持续按计划进行
答案 2 :(得分:0)