簡體   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