带有Hibernate数据保存错误的Spring MVC [英] Spring MVC with Hibernate Data Saving Error

查看:86
本文介绍了带有Hibernate数据保存错误的Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表:公司和汽车。一家公司可以有许多汽车。
我无法正确持续使用汽车。从下拉菜单中选择公司。



我的控制器

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

model.addAttribute(汽车,新汽车());
返回汽车/指数;
}

@RequestMapping(manage)
public String manage(@ModelAttribute Automotive汽车,
BindingResult结果,ModelMap模型){
模型。 addAttribute(汽车,汽车);

地图< String,String> companyList = new HashMap< String,String>();
清单<公司> companies = new ArrayList< Company>();
for(Company company:companies){
companyList.put(String.valueOf(company.getId()),company.getName());
}
model.addAttribute(companies,companyList);
automotiveService.addAutomotive(汽车);
返回汽车/指数;
}

我的视图

 < form:form action =/ Automotive / managemodelAttribute =automotive> 
名称:< form:input path =name/>
说明:< form:input path =description/>
类型:< form:input path =type/>
公司:< form:select path =companyitems =$ {companies}/>
< input type =submit/>
< / form:form>

Q1>从逻辑上看,公司ID不会被保存,保存它应该是一个公司类型的对象。我应该如何解决这个问题。我需要使用DTO还是有直接方法?



Q2>我不能直接通过公司列表来查看,而不是在控制器中创建新的地图吗?

解决方案

您可以使用公司的id作为密钥,然后使用转换器,它会自动将数据从表单转换为域对象。就像在这段代码中一样:

  public class CompanyIdToInstanceConverter实现了Converter< String,Company> {

@Inject
私人CompanyService _companyService;

@Override
public公司转换(final String companyIdStr){
return _companyService.find(Long.valueOf(companyIdStr));
}

}

在JSP中:

 < form:select path =companyitems =$ {companies}itemLabel =nameitemValue =id/ > 

如果您尚未触及此类型,您可能需要详细了解类型转换。它在Spring doc中有完美的描述(我找不到: http://static.springsource.org/spring/docs/3.0.x/reference/validation.html 第5.5段)。



我希望它会帮助你。


I have two tables: Company and 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. How should I solve this. Do I need to use a DTO or is there any direct method?

Q2> Can't i pass Company list directly to view instead of creating a new Map in controller?

解决方案

You can use id of company as a key and then use converter which will automatically convert data from form into domain object. 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:

<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).

I hope it would help you.

这篇关于带有Hibernate数据保存错误的Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆