保存子数据时传递的分离实体以保留
我在提交表单时收到此错误:
org.hibernate.PersistentObjectException: de detached entity pass to persist: com.project.pmet.model.Account;nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: releaseed entity pass to persist: com.project.pmet.model.Account
以下是我的实体:
帐户:
@Entity
@DynamicInsert
@DynamicUpdate
public class Account {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
private String login;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String email;
@ManyToOne
@JoinColumn(name = "team_id")
private Team team;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private List<Team> ownedTeams;
...
团队:
@Entity
@DynamicInsert
@DynamicUpdate
public class Team {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
private String name;
@ManyToOne
@JoinColumn(name = "owner_id", nullable = false)
private Account owner;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "team")
private List<Account> members;
...
以下是控制器的一部分:
@ModelAttribute("team")
public Team createTeamObject() {
return new Team();
}
@RequestMapping(value = "/teams/create-team", method = RequestMethod.GET)
public String getCreateTeam(@ModelAttribute("team") Team team, Principal principal) {
logger.info("Welcome to the create team page!");
Account owner = accountService.findOneByLogin(principal.getName());
team.setOwner(owner);
team.setMembers(new AutoPopulatingList<Account>(Account.class));
return "teams";
}
@RequestMapping(value = "/teams/create-team", method = RequestMethod.POST)
public String postCreateTeam(@ModelAttribute("team") Team team) {
logger.info("Team created!");
teamService.save(team);
return "redirect:/teams.html";
}
形式:
<form:form commandName="team" id="teamForm">
<div class="form-group">
<label>Name</label>
<form:input path="name" cssClass="form-control" />
</div>
<div class="form-group" id="row-template">
<label>Members</label>
<form:select path="members[0].id" cssClass="form-control" data-live-search="true" >
<form:options items="${accounts}" itemValue="id" />
</form:select>
...
</div>
<form:hidden path="owner.id" />
</form:form>
我做错了什么?