2015-11-04 93 views
0

我有一個問題,我不明白。我有幾個實體: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和形式。問題是,當我試圖從我得到的形式保存plotRequest processing failed; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.btw.spindle.model.Plot

我不知道它是如何分離,以及如何解決它。我厭倦了加入System.out來檢查是否正確地從表單中提取了必要的數據。我也有用戶實體(我沒有在這裏添加)。 userscenario之間的關係與scenarioplot之間的關係相同(一對多)。創建scenario完美工作,創建plots引發此持久性異常。

任何提示?

+0

在劇情實體中向您的「@ ManyToOne」中添加cascade = CascadeType.ALL' – Uppicharla

回答

0

發生該錯誤是因爲該對象具有ID。 Hibernate區分臨時對象和分離對象,並且只對瞬態對象(沒有ID的對象)持久工作。如果堅持總結了對象被分離(它會因爲ID的),它會返回一個錯誤

org.hibernate.PersistentObjectException:獨立實體傳遞給 堅持

我想你需要使用saveOrUpdate()代替save()/persist()

看到這個linkthis答案

+0

這不應該是一個因素。我在用戶和場景之間有着相同的關係。這兩個實體都有ID,當我創建一個帶有持久性的場景時沒有錯誤 – TergOfSky

0

有多種原因可能導致此異常。 這個異常背後的基本思想是你試圖堅持的對象不是持久狀態。

在保存或保存之前,您可以檢查Plot對象是否有alreay ID嗎?

編輯

你解決,完善。

+0

好吧,我用sys.out檢查了它,結果證明你是對的。傳遞給persist的對象有id:1 ...我只是不知道爲什麼。我沒有在表單中設置它。 – TergOfSky

0

Ok MeewU是對的。問題是因爲對象在被保存前有一個設置ID。原因是:

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

我用它來傳遞場景ID以在下一步添加ot到圖中。但事實證明,場景的ID自動設置爲圖塊ID。我將變量名稱更改爲「場景」,並且該ID不再設置並且堅持按計劃

相關問題