我是Hibernate的新手。我有两个班级“quotation”和“quotation_item” 他们的数据库结构是这样的
create table quotation
(
quotation_id int primary key auto_increment,
code varchar(20),
client_name varchar(20)
);
create table quotation_item
(
id int primary key auto_increment,
quotation_id int,
item_name varchar(20),
rate int,
qty int,
FOREIGN KEY (quotation_id) REFERENCES quotation(quotation_id)
);
Quotation.hbm.xml是这样的,
<hibernate-mapping>
<class name="com.paramatrix.pojo.Quotation" table="quotation">
<id name="quotationId" type="int" column="quotation_id">
<generator class="native" />
</id>
<property name="code" column="code" type="string" />
<property name="clientName" column="client_name" type="string" />
<set name="quotationItem" table="quotation_item" fetch="select" cascade="all">
<key>
<column name="quotation_id" not-null="true" />
</key>
<one-to-many class="com.paramatrix.pojo.QuotationItem" />
</set>
</class>
</hibernate-mapping>
QuotationItem.hbm.xml is like this,
<hibernate-mapping>
<class name="com.paramatrix.pojo.QuotationItem" table="quotation_item">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<many-to-one name="quotation" class="com.paramatrix.pojo.Quotation" cascade="save-update">
<column name="quotation_id" not-null="true" />
</many-to-one>
<property name="itemName" column="item_name" type="string" />
<property name="rate" column="rate" type="int" />
<property name="qty" column="qty" type="int" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.paramatrix.pojo.Quotation" table="quotation">
<id name="quotationId" type="int" column="quotation_id">
<generator class="native" />
</id>
<property name="code" column="code" type="string" />
<property name="clientName" column="client_name" type="string" />
<set name="quotationItem" table="quotation_item" fetch="select" cascade="all">
<key>
<column name="quotation_id" not-null="true" />
</key>
<one-to-many class="com.paramatrix.pojo.QuotationItem" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.paramatrix.pojo.QuotationItem" table="quotation_item">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<many-to-one name="quotation" class="com.paramatrix.pojo.Quotation" cascade="save-update">
<column name="quotation_id" not-null="true" />
</many-to-one>
<property name="itemName" column="item_name" type="string" />
<property name="rate" column="rate" type="int" />
<property name="qty" column="qty" type="int" />
</class>
</hibernate-mapping>
2 - ; QuotationItem.java
主要方法是
public class Quotation {
int quotationId;
String code;
String clientName;
Set<QuotationItem> quotationItem=new HashSet<QuotationItem>();
//getter& setter
}
答案 0 :(得分:0)
尝试quotationItem.setQuotation(quotation)