我有一個問題,我不明白。我有幾個實體:hibernate:堅持錯誤與分離實體
情景實體
@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";
}
我也有,服務隊,DAOS和形式。問題是,當我試圖從我得到的形式保存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
引發此持久性異常。
任何提示?
在劇情實體中向您的「@ ManyToOne」中添加cascade = CascadeType.ALL' – Uppicharla