简体   繁体   English

带有休眠数据保存错误的Spring MVC

[英]Spring MVC with Hibernate Data Saving Error

I have two tables: Company and Automotive. 我有两个表:Company和Automotive。 A company can have many automotives. 一个公司可以有很多汽车。 I am unable to persist an Automotive properly. 我无法正确坚持汽车。 Company is selected in View page from a drop down. 在“查看”页面的下拉菜单中选择了“公司”。

My Controller 我的控制器

@RequestMapping("/")
public String view(ModelMap model) {
    Map<String, String> companyList = new HashMap<String, String>();
    List<Company> companies = companyService.listAllCompanies();
    for (Company company : companies) {
        companyList.put(String.valueOf(company.getId()), company.getName());
    }
    model.addAttribute("companies", companyList);

    model.addAttribute("automotive", new Automotive());
    return "automotive/index";
}

@RequestMapping("manage")
public String manage(@ModelAttribute Automotive automotive,
        BindingResult result, ModelMap model) {
    model.addAttribute("automotive", automotive);

    Map<String, String> companyList = new HashMap<String, String>();
    List<Company> companies = new ArrayList<Company>();
    for (Company company : companies) {
        companyList.put(String.valueOf(company.getId()), company.getName());
    }
    model.addAttribute("companies", companyList);
    automotiveService.addAutomotive(automotive);
    return "automotive/index";
}

My View 我的观点

<form:form action="/Automotive/manage" modelAttribute="automotive">
    Name : <form:input path="name" />
    Description : <form:input path="description" />
    Type : <form:input path="type" />
    Company : <form:select path="company" items="${companies}" />
    <input type="submit" />
</form:form>

Q1> Logically as expected Company id would not be saved since here in view its an id but actually while saving it should be an object of type company. Q1>从逻辑上讲,不会保存公司ID,因为在这里查看其ID,但实际上在保存时它应该是公司类型的对象。 How should I solve this. 我该如何解决。 Do I need to use a DTO or is there any direct method? 我需要使用DTO还是有直接方法?

Q2> Can't i pass Company list directly to view instead of creating a new Map in controller? Q2>我不能直接通过公司列表进行查看,而不是在控制器中创建新地图吗?

You can use id of company as a key and then use converter which will automatically convert data from form into domain object. 您可以将公司ID用作密钥,然后使用转换器将数据自动从表单转换为域对象。 Just like in this code: 就像下面的代码:

public class CompanyIdToInstanceConverter implements Converter<String, Company> {

    @Inject
    private CompanyService _companyService;

    @Override
    public Company convert(final String companyIdStr) {
        return _companyService.find(Long.valueOf(companyIdStr));
    }

}

And in JSP: 在JSP中:

<form:select path="company" items="${companies}" itemLabel="name" itemValue="id"/>

You may need to read more about type conversion if you haven't touched this yet. 如果您尚未接触类型转换,则可能需要阅读更多信息。 It is perfectly described in Spring doc (I you couldn't find: http://static.springsource.org/spring/docs/3.0.x/reference/validation.html paragraph 5.5). Spring文档中对此进行了完美的描述(我找不到: http : //static.springsource.org/spring/docs/3.0.x/reference/validation.html第5.5段)。

I hope it would help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM