繁体   English   中英

休眠:与分离实体的持续错误

[英]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来检查是否从表单中正确提取了必要数据。 而且我还有用户实体(在此未添加)。 之间的关系userscenario是一样的(一对多)的之间的scenarioplot 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,并且按计划进行操作

发生错误是因为对象具有ID。 Hibernate可以区分临时对象和分离对象,并且持久化仅适用于临时对象(无ID的对象)。 如果persist断定对象已分离(由于ID,它将分离),它将返回该错误

org.hibernate.PersistentObjectException:分离的实体传递给持久化

我认为您需要使用saveOrUpdate()而不是save() / persist()

看到这个链接这个答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM