【问题标题】:Controller for @OneToMany relationship using Spring Boot使用 Spring Boot 的 @OneToMany 关系控制器
【发布时间】: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


【解决方案1】:

所以是的,cmets 确实有帮助。所以对于那些阅读它的人 - 在问愚蠢的问题之前睡在问题上并阅读日志(就像我一样:D) 问题出在jsp 页面和selection 表单内。我为conferenceId 得到了Null,如果我阅读日志我会知道的。 所以关于检查整个堆栈跟踪和更改 jsp 选择部分的 cmets 对我有用。

固定jsp选择是:

<div class="form-group">
  <label for="conferenceList">Select conference:</label>
  <form:select path="teamConference" id="conferenceList">
      <form:options items="${conference}" itemLabel="conferenceTitle"/>
  </form:select>
</div>

谢谢各位!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2022-01-24
    • 2017-10-01
    • 2016-09-18
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多