0

我有一个问题要问。尝试将新属性保存到数据库中时出现此错误。列“property_id”不能为空。尝试将要素列表保存到数据库时发生异常。由于某种原因,插入的属性与特征之间没有关系。Property_id 在对象 PropertyFeature 中不存在。

这是我的数据:

table 'property':

id
name

table 'features'

id
title
property_id [not null]

实体属性

 @Table(name = "property")
public class Property extends...{

    @Column(name = "name", nullable = true)
    private String name;    

    @OneToMany(mappedBy = "property", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<PropertyFeature> features;  


    public Property() {
    }


    public Property(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Set<PropertyFeature> getFeatures() {
        return features;
    }

    public void setFeatures(Set<PropertyFeature> features) {
        this.features = features;
    }   

}

实体属性特征

@Entity
@Table(name = "property_feature")
public class PropertyFeature extends ... {


    @Column(name = "title", nullable = false)
    private String title;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Property property;

    public PropertyFeature() {

    }

    public PropertyFeature(String title) {
        this.title = title;
    }   

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }   

    public Property getProperty() {
        return property;
    }

    public void setProperty(Property property) {
        this.property = property;
    }
}

弹簧控制器

@Controller
@RequestMapping(value="/account")
public class AccountController {

    @Autowired
    PropertyServiceImpl propertyServiceImpl;

    @RequestMapping(method = RequestMethod.GET, value = "/test")
    public String accountPage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {       


        Property property = propertyServiceImpl.load(12L);

        ArrayList<PropertyFeature> test = new ArrayList<PropertyFeature>();
        test.add(new PropertyFeature("Feature 1"));
        test.add(new PropertyFeature("Feature 2"));
        test.add(new PropertyFeature("Feature 3"));

        model.put("property", property);
        model.put("testList", test);

        return "/test";
    }   

    @RequestMapping(method = RequestMethod.POST, value = "/test")
    public String accountPost(HttpServletRequest request, HttpServletResponse response, Property property) {        

        propertyServiceImpl.update(property);       

        return "/test";
    }   

} 

JSP 格式:

<form:form method="post" commandName="property">

Name <form:input path="name" type="text" name="name" value="" />
Features
<form:select multiple="true" path="features">
    <form:options items="${testList}" itemValue="title" itemLabel="title"/>
</form:select>

<input type="submit" name="submit" value="Test" />
4

2 回答 2

1

Hibernate 需要 property_feature 上的外键来连接回ManyToOne映射的属性。您没有通过使用提供一列,@JoinColumn因此 Hibernate 使用其默认机制在您的模式中创建了一个列。这在第 2.2.5.3.1.4 节中进行了描述。

您已将 Property 映射为非可选的,因此 Hibernate 期望您始终在保存之前设置Propertya PropertyFeature。您已将 添加PropertyFeature到 Property 的列表中,但尚未设置Propertyon PropertyFeature

我建议在您的属性类上添加一个方法,如下所示:

public void addFeature(PropertyFeature feature) {
  feature.setProperty(this);
  features.add(feature);
}

然后在你的控制器中你可以写:

Property property = propertyServiceImpl.load(12L);
property.addFeature(new PropertyFeature("Feature 1"));
于 2012-09-28T20:41:43.120 回答
0

我看到了问题所在。为了使它成为我想要的方式,我需要第三个表来连接属性和功能。在这种情况下,我可以指定

@JoinTable(name="property_features", joinColumns={@JoinColumn(name="property_id")}, inverseJoinColumns={@JoinColumn(name="id")})

在这种情况下,休眠创建第三个表,我不必将对象属性设置为每个 PropertyFeature 实例。这很好,但在这种情况下我不喜欢第三张桌子。

我想我会将我的 Property 实体方法 setFeatures 更新为类似的东西

public void setFeatures(Set<PropertyFeature> features) {
        for(PropertyFeature pf : features)
            pf.setProperty(this);
        this.features = features;
    }

经测试,按预期工作。如果有更好的方法只用两张桌子来处理它,请告诉我。谢谢

于 2012-09-28T22:06:19.187 回答