Spring不保存另一个表的外键

时间:2014-07-12 06:38:55

标签: java mysql spring hibernate spring-mvc

我的表名City看起来像

package com.spring.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "Cities")
public class City {

    @Id
    @Column(name = "city_ID")
    @GeneratedValue
    private Integer id;

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

    public Integer getId() {
        return id;
    }

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

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

    public String getName() {
        return name;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "city")
    private Set<CompanyData> companyDatas = new HashSet<CompanyData>(0);

    public void setCompanyDatas(Set<CompanyData> companyDatas) {
        this.companyDatas = companyDatas;
    }

    public Set<CompanyData> getCompanyDatas() {
        return companyDatas;
    }

}

我有另一个表名Company

package com.spring.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="CompanyDatas")
public class CompanyData {

    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

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

    public Integer getId() {
        return id;
    }

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

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

    public String getCompanyName() {
        return name;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "city_ID")
    private City city;

    public void setCity(City city) {
        this.city = city;
    }   
    public City getCity() {
        return city;
    }

}

并且我有一个表单来存储公司信息,但它没有在表中保存city_ID,尽管所有其他信息都准确存储。

表单看起来像

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Company Data Manager | Company Data</title>
</head>
<body>

<h2>Company Data Manager</h2>

<form:form method="post" action="add.html" commandName="companyData">
        <table>
            <tr>
                <td><form:label path="city"><spring:message code="label.city"/></form:label></td>
                <td><form:select name="city" path="city" >
                    <c:forEach items="${cityList}" var="city">
                        <option value="${city}">${city.name}
                    </c:forEach>
                    </form:select>
                </td>

            </tr>
            <tr>
                <td><form:label path="name"><spring:message code="label.name"/></form:label></td>
                <td><form:input path="name" /></td> 
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="<spring:message code="label.addcompanyData"/>"/>
                </td>
            </tr>
        </table>  
</form:form>
</body>
</html> 

我不知道问题的原因在于city_ID表中没有存储CompanyDatas的原因。我是春天休眠的新手。请指导我,谢谢

编辑公司控制器中保存公司记录的方法

@RequestMapping(value = "/companyData/add", method = RequestMethod.POST)
public String addCompany(@ModelAttribute("companyData")
    CompanyData companyData, BindingResult result) {
    companyDataService.addCompanyData(companyData);
    return "redirect:/companyData/index";
}

,实施是

public void addCompanyData(CompanyData companyData) {
    sessionFactory.getCurrentSession().save(companyData);
}

2 个答案:

答案 0 :(得分:0)

在用于保存城市的Dao课程中,您需要先将City对象保存到数据库中,然后遍历集合:Set<CompanyData> companyDatas,保存单个companyData

类似的东西:

// make sure you've set each companyData to be owned by the city object

city.getCompanyDatas.add(companyData);  // you will probably need to do this as many times as the size of the Set
companyData.setCity(city);   // set the owner to the city object

Transaction tx = session.beginTransaction();
session.save(city)           //save city object first so you will get the cityId for companyData
for (CompanyData cd : companyDatas)
{
    session.save(cd);
}
tx.commit();

答案 1 :(得分:-1)

您的表单companyData的模型与您的Entity不一致。     使用不同的Model类,如CompanyDataModel

public class CompanyDataModel {
  private String name;
  private String city;
// GETTERS AND SETTERS

}

您的表单应如下所示:

<form:form method="post" action="add.html" commandName="companyDataModel">
        <table>
            <tr>
                <td><form:label path="city"><spring:message code="label.city"/></form:label></td>
                <td><form:select name="city" path="city" >
                    <c:forEach items="${cityList}" var="city">
                        <option value="${city.id}">${city.name}
                    </c:forEach>
                    </form:select>
                </td>

            </tr>
            <tr>
                <td><form:label path="name"><spring:message code="label.name"/></form:label></td>
                <td><form:input path="name" /></td> 
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="<spring:message code="label.addcompanyData"/>"/>
                </td>
            </tr>
        </table>  
</form:form>
控制器:

@RequestMapping(value = "/companyData/add", method = RequestMethod.POST)
public String addCompany(@ModelAttribute("companyDataModel")
    CompanyDataModel companyDataModel, BindingResult result) {  
    companyDataService.addCompanyData(companyDataModel);
    return "redirect:/companyData/index";
}

保存CompanyData的服务方法:

public void addCompanyData(CompanyDataModel companyDataModel) {
CompanyData comData = new CompanyData();
       comData.setComapnyName(companyDataModel.getName());
       session = sessionFactory().getCurrentSession();
       City city =  (City) session.get(City.class, Integer.parseInt(companyDataModel.getCity));
       campanyData.setCity(city);
       session.save(companyData);
 }