提交表單時,我得到這個錯誤:獨立實體傳遞堅持錯誤
org.hibernate.PersistentObjectException:通過堅持脫離實體:com.project.pmet.model.Account;嵌套的例外是javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:通過堅持脫離實體: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>
我在做什麼錯?
爲第一個解決方案投票,這對我有幫助。 – vtomic85