1、数据库结构
customer表:{ customerId , customerName}
orders表:{ orderId , orderName , idCustomer},其中idCustomer是外键,表示customer的ID。
(本人采用的是MS SQL SERVER 2000个人版,刚开始时新建了一个order表,结果老是报错,说' order '附近有语法错误,后来才明白order是数据库的关键字。因此,千万不要与数据库关键字重名!)
2、Customer.hbm.xml
<hibernate-mapping>
<class name="com.persistent.Customer" table="customer">
<id name="id" column="customerId" unsaved-value="null">
<generator class="increment"></generator>
</id>
<property name="name" column="customerName"></property>
<set name="orders" cascade="all" lazy="false">
<key column="idCustomer"></key>
<one-to-many class="com.persistent.Order"/>
</set>
</class>
</hibernate-mapping>
3、Order.hbm.xml
<hibernate-mapping>
<class name="com.persistent.Order" table="orders">
<id name="id" column="orderId" unsaved-value="null">
<generator class="increment"></generator>
</id>
<property name="name" column="orderName"></property>
<many-to-one name="customer"
column="idCustomer"
class="com.persistent.Customer"
lazy="false"
cascade="all">
</many-to-one>
</class>
</hibernate-mapping>
4、Customer.java和Order.java
public class Customer ...{
private Long id;
private String name;
private Set orders = new HashSet();
...
}
public class Order ...{
private Long id;
private String name;
private Customer customer;
...
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/thinkGhoster/archive/2007/10/24/1842274.aspx
customer表:{ customerId , customerName}
orders表:{ orderId , orderName , idCustomer},其中idCustomer是外键,表示customer的ID。
(本人采用的是MS SQL SERVER 2000个人版,刚开始时新建了一个order表,结果老是报错,说' order '附近有语法错误,后来才明白order是数据库的关键字。因此,千万不要与数据库关键字重名!)
2、Customer.hbm.xml
<hibernate-mapping>
<class name="com.persistent.Customer" table="customer">
<id name="id" column="customerId" unsaved-value="null">
<generator class="increment"></generator>
</id>
<property name="name" column="customerName"></property>
<set name="orders" cascade="all" lazy="false">
<key column="idCustomer"></key>
<one-to-many class="com.persistent.Order"/>
</set>
</class>
</hibernate-mapping>
3、Order.hbm.xml
<hibernate-mapping>
<class name="com.persistent.Order" table="orders">
<id name="id" column="orderId" unsaved-value="null">
<generator class="increment"></generator>
</id>
<property name="name" column="orderName"></property>
<many-to-one name="customer"
column="idCustomer"
class="com.persistent.Customer"
lazy="false"
cascade="all">
</many-to-one>
</class>
</hibernate-mapping>
4、Customer.java和Order.java
public class Customer ...{
private Long id;
private String name;
private Set orders = new HashSet();
...
}
public class Order ...{
private Long id;
private String name;
private Customer customer;
...
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/thinkGhoster/archive/2007/10/24/1842274.aspx