【发布时间】:2018-11-08 19:20:35
【问题描述】:
我正在尝试解决 Spring Boot 和数据库问题。
所以我有 2 个具有 @OneToMany 关系的实体:
@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int teamId;
@Column
private String teamTitle;
@Column
private String teamCity;
@ManyToOne
@JoinColumn(name = "conferenceId", nullable = false)
private Conference teamConference;
public Team() { super(); }
//some getters and setters
}
第二个:
@Entity
public class Conference {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int conferenceId;
private String conferenceTitle;
@OneToMany(mappedBy = "teamId")
private List<Team> conferenceTeams;
public Conference() {
super();
}
//some getters and setters
}
Jsp页面:
<body>
<form:form method="post" modelAttribute="team">
<div>
<form:label path="teamTitle">Title</form:label>
<form:input path="teamTitle" type="text"/>
<form:label path="teamCity">City</form:label>
<form:input path="teamCity" type="text"/>
//DAHELL IS HERE
<div class="form-group">
<label for="conferenceList">Select conference:</label>
<select class="form-control" id="conferenceList">
<c:forEach items="${conference}" var="conf">
<option>${conf.conferenceTitle}</option>
</c:forEach>
</select>
</div>
<button type="submit" class="btn btn-success">Add</button>
</div>
</form:form>
// jquery etc
</body>
和控制器类:
@Controller
public class TeamsController {
@Autowired
private TeamDAO teamDAO;
@Autowired
private ConferenceDAO conferenceDAO;
@RequestMapping(value = "/schedule", method = RequestMethod.GET)
public String showSchedule(ModelMap model) {
model.put("conferences", conferenceDAO.findAll());
model.put("teams", teamDAO.findAll());
return "schedule";
}
@RequestMapping(value = "/new-team", method = RequestMethod.GET)
public String addNewTeam(ModelMap model) {
model.addAttribute("conference", conferenceDAO.findAll());
model.addAttribute("team", new Team());
return "new-team";
}
@RequestMapping(value = "/new-team", method = RequestMethod.POST)
public String addTeam(ModelMap model, Team newTeam) {
teamDAO.save(newTeam);
return "redirect:/schedule";
}
}
ConferenceDAO 和 TeamDAO 只是从 JpaRepository 扩展而来的接口。
所以我想了解的是如何添加新的Team。我通过jsp 页面插入标题和城市,并且我应该选择这个团队属于哪个会议。但是当我按下add 按钮时,我得到了
There was an unexpected error (type=Internal Server Error, status=500).
No message available
我做错了什么?我相信selecting 部分包含在jsp 页面中。而且我 100% 确定我在Controller 课程中遗漏了一些东西。不知何故,我应该将新团队保存到我的数据库中,Conference 列也应该显示它包含这个新团队。
如果你能告诉我挖掘的方法,我真的很感激。
【问题讨论】:
-
请包含整个堆栈跟踪
-
调试并找出你在什么地方出错?表单中的操作在哪里?
-
可能无法为 addTeam post 方法创建团队。当您不确定问题出在哪里时,您可能应该从堆栈跟踪而不是这么多代码开始。谢谢。请看MCVE
标签: java database spring-mvc spring-boot spring-data-jpa