gpt4 book ai didi

java - hibernate :分离实体的持久错误

转载 作者:行者123 更新时间:2023-11-30 03:11:34 25 4
gpt4 key购买 nike

我有一个不明白的问题。我有一些实体:

场景实体

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

和 Controller

@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 时,我得到: 请求处理失败;嵌套异常是 org.hibernate.PersistentObjectException:传递给持久化的分离实体:com.btw.spindle.model.Plot

我不明白它是如何分离的以及如何修复它。我厌倦了添加 System.out 来检查是否从表单中正确提取了必要的数据。我还有用户实体(我没有在此处添加)。 userscenario 之间的关系与 scenarioplot 之间的关系相同(一对多)。 scenario 的创建工作完美,而 plots 的创建会抛出此持久异常。

有什么提示吗?

最佳答案

好吧 MeewU 是对的。问题是因为该对象在持久化之前有一个设置的 id。原因是:

@RequestMapping(value = { "/scenario-{id}-newPlot" }, method = RequestMethod.GET)
public String newPlot(ModelMap model, @PathVariable int id) {

我用它来传递场景 ID,以便在下一步中将 ot 添加到绘图中。但事实证明,场景的id被自动设置为情节id。我将变量名称更改为“scenario”,并且不再设置 id,并按计划持续进行

关于java - hibernate :分离实体的持久错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521033/

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