Http Status 400外键插入错误

时间:2015-01-13 18:01:59

标签: hibernate jsp spring-mvc controller foreign-keys

我在将外键添加到表中时遇到问题,有什么问题?请帮忙。

视图中的部分代码:

    <td>
        <form:select path="type">
           <form:options items="${typeList}" itemValue="id" itemLabel="name" />
        </form:select>
    </td>

实体:

@Entity
@Table(name = "myTable")
public class Type {

private int id;
private String name;

public Type() {
    name = null;
}

public Type(Type type) {
    name = type.getName();
}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")

@Column(name = "id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Column(name = "name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

private Set<Store> stores = new HashSet<Store>(
        0);
@OneToMany
public Set<Store> getStore(){
    return this.stores;
}
public void setStore(Set<Store> stores){
    this.stores = stores;
}

}

在商店实体中:

.....
private Type type;
@ManyToOne//(fetch = FetchType.LAZY)
@JoinColumn(name = "idType")
public Type getType(){
    return type;
}
public void setType(Type type){
    this.type = type;
}
....

在控制器中我只添加类型列表,并在视图中调用此列表.. 在控制器中:

        @RequestMapping(value = "/regStore", method = RequestMethod.GET)
public ModelAndView addStore() throws SQLException {
    ModelAndView modelAndView = new ModelAndView("Store/regStore", "command", new Store());

    Store storeForm = new Store();
    modelAndView.addObject("storeForm", storeForm);

    modelAndView.addObject("typeList", typeService.getAllTypes());

    return modelAndView;
}

//post method create store
@RequestMapping(value = "/regStoreSuccessful", method = RequestMethod.POST)
public ModelAndView addStorePost(@ModelAttribute("storeForm") Store store, BindingResult bindingResult, Principal principal) throws SQLException {
    ModelAndView modelAndView = new ModelAndView("redirect:body");

    User user;
    user = userService.getUserByEmail(principal.getName());

    store.setUser(user);

    storeService.addStore(store);
    return modelAndView;
}

Web.xml,Spring-config,安全性 - 一切都很好,但是有什么问题,prt scr问题: enter image description here

1 个答案:

答案 0 :(得分:0)

解决。我刚刚将BindindResult添加到我的POST方法中;)