Spring form persist复合对象列表

时间:2016-09-02 15:06:21

标签: hibernate spring-mvc spring-form

伙计们!如何使用复合类型列表保存实体。例如,我有:

@Entity
@Table(name = "distributor")
public class Distributor {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;

@Column(name = "city")
private String city;

@Column(name = "address")
private String address;


@OneToMany(cascade = CascadeType.ALL)
List<Exhibitor> exhibitor = new ArrayList<Exhibitor>();

@OneToMany(cascade = CascadeType.ALL)
List<Merchandiser> merchandiser = new ArrayList<Merchandiser>();

.. getter and setters。

提交控制器:

@RequestMapping(value = "submission", method = RequestMethod.GET)
    public String showSubmission(Model model) {

    Query Exhibitor = sessionFactory.getCurrentSession().createQuery("from Exhibitor");
    List<Merchandiser> listExhibitor = Exhibitor.getResultList();
    model.addAttribute("listOfExhibitors", listExhibitor);

    Query Merchandisers = sessionFactory.getCurrentSession().createQuery("from Merchandiser");
    List<Merchandiser> listMerchandisers = Merchandisers.getResultList();
    model.addAttribute("listMerchandisers", listMerchandisers);

    Query query = sessionFactory.getCurrentSession().createQuery("from Distributor");
    List<Distributor> listDistributors = query.getResultList();
    model.addAttribute("listDistributors", listDistributors);

    model.addAttribute("submission", new Distributor());

    return "submission";
}

@RequestMapping(value = "submission", method = RequestMethod.POST)
public String show(Model model, @ModelAttribute("submission") Distributor submission, BindingResult error) {


    sessionFactory.getCurrentSession().save(submission);

    /*Distributor distributor = sessionFactory.getCurrentSession().get(Distributor.class, 1L);*/


    return "redirect:submission";
}

和JSP,每当实体(Merchandiser或参展商或经销商更新)时更新JSP现在正在运行,但我如何更新(或放入)列表Exhibitor = new ArrayList(); < / strong>从jsp表单提交:     

    <form:select path="name">
        <c:forEach var="distributor" items="${listDistributors}"  >
            <form:option value="${distributor.id}">${distributor.name}</form:option>
        </c:forEach>
    </form:select>

    <form:select path="exhibitor">
        <c:forEach var="exhibitors" items="${listOfExhibitors}"  >
            <form:option value="${exhibitors.id}">${exhibitors.name}</form:option>
        </c:forEach>

    </form:select>
<input type="submit" value="Submission" />
</form:form>

1 个答案:

答案 0 :(得分:0)

首先我在代码中看到错误

List<Merchandiser> listExhibitor = Exhibitor.getResultList();它应该是 List<Exhibitor> listExhibitor = Exhibitor.getResultList();

其次,您没有遵循变量命名约定。变量名应始终以小字符开头。

第三,你的JSP似乎对我不正确。发布完整的JSP页面。当您一次保存一个分发服务器时,为什么要添加分发服务器列表。