tb_productAttribute
+-----------+-------------+-------+
| productId | attributeId | value |
+-----------+-------------+-------+productId、 attributeId一起构成该表主键,又分别作为外键参照如下两个表的id主键:tb_product
+----+------+
| id | name |
+----+------+tb_attribute
+----+------+
| id | name |
+----+------+
tb_procuctAttribute表对应类和xml:
public class ProductAttribute {
private ProductAttributePK compId;//复合主键类,都明白就不说了
private String value;
private Product product;
private Attribute attribute;
……
}
<composite-id name="compId" class="ProductAttributePK">
<key-property name="productId"/>
<key-property name="attributeId"/>
</composite-id>
<property name="value"/>
<many-to-one
name="product"
class="Product"
insert="false"
update="false">
<column name="productId" not-null="false"/>
</many-to-one> ……
tb_product对应类:
public class Product {
private Long id;
private Set<ProductAttribute> productAttribute;
……
}
<id name="id" column="productId">
<generator class="native"/>
</id>
<set name="productAttribute" inverse="true" cascade="all">
<key column="productId" not-null="false"/>
<one-to-many class="ProductAttribute"/>
</set>
……    tb_attribute对应类与tb_product对应类相似,不贴了
然后我保存 Product,并设想着向tb_product表中和tb_productAttribute表中插入记录,代码如下:
Product product=new Product();
product.setName(productForm.getName());

Set pas=new HashSet();
ProductAttribute pa1=new ProductAttribute();
ProductAttribute pa2=new ProductAttribute(); pa1.setCompId(new ProductAttributePK(product.getId(),1);
pa1.setProduct(product);
pa1.setAttribute(new Attribute(1,"颜色"));
pa1.setValue("绿色"); pa2.setCompId(new ProductAttributePK(product.getId(),2);
pa2.setProduct(product);
pa2.setAttribute(new Attribute(2,"尺寸"));
pa2.setValue("24英寸");

pas.add(pa1); pas.add(pa2);
product.setProductAttribute(pas);
productServer.add(product); //保存Product,具体代码都知道,就是通过session.save()来保存的
  结果tb_product表中插入记录了,可是提示异常说“Column 'productId' cannot be null”是怎么回事?